Copy from fixed range does not work

F

Frank Situmorang

Hello,

I am already tired, I checked and tried to follow all Codes as suggested on
Ron De Bruins's website. Filepath and the fille name have succecefully
copied, but the fixed range that I mensioned in the code is NOT copied.

I am not too good in the VBA, I appreciate if anyone can help me. The
following is the VBA:
Private Declare Function SetCurrentDirectoryA Lib _
"kernel32" (ByVal lpPathName As String) As Long

Sub ChDirNet(szPath As String)
SetCurrentDirectoryA szPath
End Sub
Sub MergeSpecificWorkbooks()
Dim MyPath As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
Dim SaveDriveDir As String
Dim FName As Variant


' Set application properties.
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With

SaveDriveDir = CurDir
' Change this to the path\folder location of the files.
ChDirNet "\\Admin-hdd\budget-contr\BUDGET CONTROL M\BUDGET 2009"

FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*),
*.xl*", _
MultiSelect:=True)
If IsArray(FName) Then

' Add a new workbook with one sheet.
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 2


' Loop through all files in the myFiles array.
For FNum = LBound(FName) To UBound(FName)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & FName(FNum), _
Password:="KIKI", WriteResPassword:="KIKI", UpdateLinks:=0)

On Error GoTo 0

If Not mybook Is Nothing Then

On Error Resume Next
With mybook.Worksheets("SUMMARY")
Set sourceRange =
..Range("C7,C8,E7,D118,H5,D63,E63,D70,F70," & _
"D80,F80,D102,F102,D108,D109")
End With

If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
' If the source range uses all columns then
' skip this file.
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0

If Not sourceRange Is Nothing Then

SourceRcount = sourceRange.Rows.Count

If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "There are not enough rows in the target
worksheet."
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else

' Copy the file name in column A.

With sourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = FName(FNum)
End With

' Set the destination range.
Set destrange = BaseWks.Range("B" & rnum)

' Copy the values from the source range

' to the destination range.
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count,
..Columns.Count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If

Next FNum
BaseWks.Columns.AutoFit
End If

ExitTheSub:
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
ChDirNet SaveDriveDir
End Sub

Thanks a lot in advance

Frank
 
J

joel

Frank: It is hard to find this problem just looking at the code. I can't
tell what you are referding to as the FIXED RANGE. The code is dependant on
the data in the worksheet and the problem may be related to the data on the
worksheet. I recommend that you put a break point on this line and then step
through the code

If Not sourceRange Is Nothing Then

Click on line with mouse and then Press F9 to add break point. Then step
through the code by pressing F8. You can see the value of the variables by
hoving the mouse over the variables and add a Watch point by highlighting the
variable and right click mouse (select ADD WATCH).

Let me know when you better define where the error is occuring.
 
F

Frank Situmorang

Ron,

Thanks for your help. I already assingn a button for the files without
password based on your 1st example, now ithe the ones with password, I want
to assingn another button, is there any way to modify your code below to
allow it to have diffrent area?

I am sure you have the clue, if you can just modify the code below.

Thanks in advance

Frank
 
F

Frank Situmorang

Joel:

The fixed range that I refer to is
..Range("C7,C8,E7,D118,H5,D63,E63,D70,F70,D80,F80,D102,F102,D108,D109")

For every wokbook have the same range, that is why I said it fixed.

Ron has responded this thread and he said because my range is not in one
area. Yes I have to admit that it is scatterred. But I think if in manual we
can choose the range that we want to copy by pressing CTRL, I think it should
be able to do it in Macro.

I have tried your suggestion, the cell that I want to add watch saiys " want
be able to show watch there"

Thanks for your help

Frank
 
F

Frank Situmorang

Joel:

as per your instruction, I have add watch on this range, I wonder why the
value is empty:
Set sourceRange =
..Range("C7,C8,E7,D118,H5,D63,E63,D70,F70," & _
"D80,F80,D102,F102,D108,D109")

Did I miss something?

Thanks

Frank
 
F

Frank Situmorang

Joel:

After trial and erro, yes I am able to add watch and try to see, the content
of the cells I mentioned in the range, in fact in the watch window says the
value is " expression not defined in context", as a result no contents of the
rage copied to the new sheet.

Do you know what it means?

Frank
 

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