Error in Macro

D

drinese18

I'm trying to create a macro that will copy data from one sheet to another.
It should bring up a InputBox where you would input the name of the workbook,
find it within the specified directory and paste the data to that workbook,
but I'm getting some errors within it, my code can be seen below:

Sub CopyData()

Dim rngCopyFrom As Range
Dim wbkCopyTo As Workbook
Dim rngCopyTo As Range
Dim inpData As Integer

inpData = InputBox(Prompt:="Enter Market Name", _
Title:="Market Name", Default:="Enter Market Name here")

If inpData = "Enter Market Name here" Or _
inpData = vbNullString Then
Exit Sub
Else

On Error Resume Next
Set wbkCopyTo = Workbooks(inpData)
If wbkCopyTo Is Nothing Then
Set wbkCopyTo = Workbooks.Open("C:\Documents and
Settings\jermaine_wanyou\Desktop\FACTBOOK SYSTEM\New Folder\")
On Error GoTo 0
If wbkCopyTo Is Nothing Then
MsgBox "Sorry... Can't find the destination file."
Else
Set rngCopyFrom =
ThisWorkbook.Sheets("Sheet1").Range("B4:M17")
Set rngCopyTo = wbkCopyTo.Sheets("Sheet1").Range("B4:M17")
End If
End If

End If

rngCopyTo.Value = rngCopyFrom.Value
wbkCopyTo.Close

End Sub

I'm very lost here, I'm not sure where exactly the error is coming from but
much help will be appreciated
 
J

Joel

InpData is defined as an Integer but you probably need to input a string

from
Dim inpData As Integer
to
Dim inpData As String
 
D

drinese18

It still says data type mismatch, but I also wanted it to copy data from a
closed workbook, for instance, I wanted so that when I type the name of the
file in the input box it will open the workbook which i typed in the input
box and then copy the range that's already specified in the macro to the main
workbook, is there anyway of doing that as well?
 
J

Joel

It help if you can tell me which line of code fails! Yes, you can copy data
from one workbook to another very easily. It looks like your code should
work. Lets get past the first error and see if the rest of the code works.
 
D

drinese18

Actually nevermind for the first error, I corrected it, but for the second
part I tried utilizing it to find the workbook based what the name of the
workbook I typed in the input box, when it gets that information it should
copy the data from the workbook I typed in the inputbox, copy it and paste it
to the main workbook, but instead it's just skipping over that line of code
the one that enables it to copy and just going straight to the one that says

"If wbkCopyTo Is Nothing Then
MsgBox "Sorry... Can't find the destination file."

So kind of at a loss here
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top