I am having a small, but irritating problem trying to extract data from one workbook into another, the extract is fine as far as it goes, if there is content in the source worksheet cell. No problem.
But if there is no data in the cell, then it causes the data being extracted into the destination sheet to become unsychronised.
I have attempted to correct this by setting a default cell content to be used if the source cell is blank. But while it works for a single cell range, it will not work for a multiple cell range.
The multiple cell range has contiguous and non-contiguous cells stated. ("E26,E28,E30,E32:40") etc.
The following is the code I have so far. The problem area is indicated. In the main macro there are 117 cells involved. I have cut it short for the sake of clarity here.
code
sub importWkRptData()
Dim X as Long, Z as Variant, Y as Variant
Dim Bk as workbook, Sh as worksheet, Sh1 as worksheet
Dim rng as range
Dim rng1 as range
set sh = workbooks("WRTest1a.xls").worksheets("Sheet1") 'Destination
application.screenupdating = false
'Get the fields to be used for extracting data.
z = application.getopenfilename(filefilter:="Excel Files (*.xls), *.xls", Multiselect:= true
if not isarray(z) then
msgbox "Nothing was selected"
exit sub
end if
For x = 1 to Ubound(z)
Set Bk = Workbooks.Open(z(x))
on error resume next
set sh1 = Bk.worksheets("Weekly report") ' The data source sheet in the source report
on error goto 0
If not sh1 is nothing then
set rng = sh1.range("C2")
Set rng1 = sh.cells(rows.count,1).end(xlUp) (2)
if rng = "" then
rng = "No site name entered"
rng1.copy
rng1.pastespecial xlvalues
else
rng.copy
rng1.pastespecial xlvalues
end if
set rng = sh1.range("C6")
Set rng1 = sh.cells(rows.count,2).end(xlUp) (2)
if rng = "" then
rng = "No Date entered"
rng1.copy
rng1.pastespecial xlvalues
else
rng.copy
rng1.pastespecial xlvalues
end if
set rng = sh1.range("E2")
Set rng1 = sh.cells(rows.count,3).end(xlUp) (2)
if rng = "" then
rng = "No FT name entered"
rng1.copy
rng1.pastespecial xlvalues
else
rng.copy
rng1.pastespecial xlvalues
end if
'==========Problem area===============================
set rng = sh1.range("E26,E28,E30,E32:40")
Set rng1 = sh.cells(rows.count,4).end(xlUp) (2)
if rng = "" then
rng = "--"
rng1.copy
rng1.pastespecial xlvalues, transpose := true
'======================================================
else
rng.copy
rng1.pastespecial xlvalues, transpose := true
end if
BK.CLOSE
next X
mSGBOX "The Data import is complete"
End sub
/code
'If at first you don't succeed, then your hammer is below specifications'
But if there is no data in the cell, then it causes the data being extracted into the destination sheet to become unsychronised.
I have attempted to correct this by setting a default cell content to be used if the source cell is blank. But while it works for a single cell range, it will not work for a multiple cell range.
The multiple cell range has contiguous and non-contiguous cells stated. ("E26,E28,E30,E32:40") etc.
The following is the code I have so far. The problem area is indicated. In the main macro there are 117 cells involved. I have cut it short for the sake of clarity here.
code
sub importWkRptData()
Dim X as Long, Z as Variant, Y as Variant
Dim Bk as workbook, Sh as worksheet, Sh1 as worksheet
Dim rng as range
Dim rng1 as range
set sh = workbooks("WRTest1a.xls").worksheets("Sheet1") 'Destination
application.screenupdating = false
'Get the fields to be used for extracting data.
z = application.getopenfilename(filefilter:="Excel Files (*.xls), *.xls", Multiselect:= true
if not isarray(z) then
msgbox "Nothing was selected"
exit sub
end if
For x = 1 to Ubound(z)
Set Bk = Workbooks.Open(z(x))
on error resume next
set sh1 = Bk.worksheets("Weekly report") ' The data source sheet in the source report
on error goto 0
If not sh1 is nothing then
set rng = sh1.range("C2")
Set rng1 = sh.cells(rows.count,1).end(xlUp) (2)
if rng = "" then
rng = "No site name entered"
rng1.copy
rng1.pastespecial xlvalues
else
rng.copy
rng1.pastespecial xlvalues
end if
set rng = sh1.range("C6")
Set rng1 = sh.cells(rows.count,2).end(xlUp) (2)
if rng = "" then
rng = "No Date entered"
rng1.copy
rng1.pastespecial xlvalues
else
rng.copy
rng1.pastespecial xlvalues
end if
set rng = sh1.range("E2")
Set rng1 = sh.cells(rows.count,3).end(xlUp) (2)
if rng = "" then
rng = "No FT name entered"
rng1.copy
rng1.pastespecial xlvalues
else
rng.copy
rng1.pastespecial xlvalues
end if
'==========Problem area===============================
set rng = sh1.range("E26,E28,E30,E32:40")
Set rng1 = sh.cells(rows.count,4).end(xlUp) (2)
if rng = "" then
rng = "--"
rng1.copy
rng1.pastespecial xlvalues, transpose := true
'======================================================
else
rng.copy
rng1.pastespecial xlvalues, transpose := true
end if
BK.CLOSE
next X
mSGBOX "The Data import is complete"
End sub
/code
'If at first you don't succeed, then your hammer is below specifications'