PC Review


Reply
Thread Tools Rate Thread

Copy from fixed range does not work

 
 
Frank Situmorang
Guest
Posts: n/a
 
      12th May 2009
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
 
Reply With Quote
 
 
 
 
joel
Guest
Posts: n/a
 
      12th May 2009
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.

"Frank Situmorang" wrote:

> 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

 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      12th May 2009
Hi Frank

You range is not one area
Use my add-in to copy a range with more areas
http://www.rondebruin.nl/merge.htm


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm




"Frank Situmorang" <(E-Mail Removed)> wrote in message
news:99422C55-3C9B-4E98-B2A3-(E-Mail Removed)...
> 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


 
Reply With Quote
 
Frank Situmorang
Guest
Posts: n/a
 
      12th May 2009
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


"Ron de Bruin" wrote:

> Hi Frank
>
> You range is not one area
> Use my add-in to copy a range with more areas
> http://www.rondebruin.nl/merge.htm
>
>
> --
>
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
>
>
>
>
> "Frank Situmorang" <(E-Mail Removed)> wrote in message
> news:99422C55-3C9B-4E98-B2A3-(E-Mail Removed)...
> > 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

>
>

 
Reply With Quote
 
Frank Situmorang
Guest
Posts: n/a
 
      13th May 2009
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

"joel" wrote:

> 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.
>
> "Frank Situmorang" wrote:
>
> > 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

 
Reply With Quote
 
Frank Situmorang
Guest
Posts: n/a
 
      13th May 2009
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

"joel" wrote:

> 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.
>
> "Frank Situmorang" wrote:
>
> > 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

 
Reply With Quote
 
Frank Situmorang
Guest
Posts: n/a
 
      13th May 2009
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

"joel" wrote:

> 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.
>
> "Frank Situmorang" wrote:
>
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy a Range of Data to another Work Sheet =?Utf-8?B?VGhlbyBEZWdy?= Microsoft Excel Programming 13 19th Jun 2007 12:59 PM
Copy fixed range of multiple sheets into new workbook A. Karatas Microsoft Excel Programming 9 12th Jun 2007 08:52 AM
how to copy formulas from one range to another range in other work =?Utf-8?B?TWlyaQ==?= Microsoft Excel Programming 1 6th Jun 2007 03:52 PM
Re: Copy a named range from an installed add-in while in anothter work Chip Pearson Microsoft Excel Programming 0 5th Jan 2007 10:58 PM
Copy and Paste a formula as Values in a fixed Range as a loop statement using VBA Edward S Microsoft Excel Discussion 1 28th Jun 2004 12:54 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:29 AM.