finding out what is the range with Endxl - excel

First get the range of non-empty cells from Source workbook. Then select similar range of cells in Destination Workbook. How can this be achieved?
Full Code::
~~~~~~~~~~~
Public Sub ConvertTo_K()
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Workbooks("Source.xls").Worksheets("Source").Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp).Resize(, 1))
Set rng2 = Workbooks("Destination.xls").Worksheets("Destination").Range(rng1.Address)
rng2.Value = Round(rng1.Value / 1000, 2)
'At this point, an error message of Type Mismatch pops up (Due to different ranges of rng1 and rng2). Do i have to use a loop? How to do that?
End Sub

Round(rng1.Value / 1000, 2) will not work because rng1 is a collection of ranges and can be accessed by rng1.Cells(1).value
rng1.Value is not valid.
You may loop thru each cell of rng2 and apply the round formula.
Public Sub ConvertTo_K()
Dim rng1 As Range
Dim rng2 As Range
Dim RoundRange As Range
Dim rngVal As Double
Dim SourceWkb As Workbook
Set SourceWkb = Workbooks("Source.xls")
Dim SourceSht As Worksheet
Set SourceSht = SourceWkb.Worksheets("Source")
With SourceSht
Set rng1 = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp).Resize(, 1))
End With
Dim DestinWkb As Workbook
Set DestinWkb = ThisWorkbook 'Workbooks("Destination.xls")
Dim DestinSht As Worksheet
Set DestinSht = DestinWkb.Worksheets("Destination")
With DestinSht
Set rng2 = .Range(rng1.Address)
End With
'rng2.Value = Round(rng1.Value / 1000, 2) This wont work
rng1.Copy rng2
For Each cell In rng2
cell.Value = Round(cell / 1000, 2)
Next
End Sub

If your rng1 works fine you could do it this way:
set rng2 = Workbooks("Destination.xls").Worksheets("Destination").Range(rng1.address)

Related

Using for each with a worksheet reference

If ws below has both the range object and cell property:
dim ws as worksheet
set ws = sheets("sheet_1")
x = ws.range("A1")
x = ws.cells(1,1)
Why doesn't this work:
dim cel as range
dim rng as range
set rng = 'some range
for each cel in rng
ws.cel.value = "foo"
next
I want to ensure the right sheet is being referenced, and it seems logical to use ws as part of the cel reference to ensure the sheets reference is explicit.
You need to include how you set the rng, that's a vital bit of info to help troubleshoot. But generally, this should work:
Sub t()
Dim ws As Worksheet
Set ws = Sheets("sheet_1")
Dim cel As Range, rng As Range
Set rng = ws.Range("A1:C100")
For Each cel In rng.Cells
cel.Value = "foo"
Next
End Sub

VBA Adding another variable to a loop

I have a method CreatePage(). I would like to loop through variables to pass through the method. I have managed to get it working for one variable:
Sub main()
Dim rng As Range
Dim aSht As Worksheet
Set aSht = ThisWorkbook.Sheets("Sheet1")
For Each rng In aSht.Range("A5:A8")
CreatePage(rng)
Next rng
End Sub
I need it to loop as follows:
CreatePage("A5", "C6")
CreatePage("A6", "C7")
CreatePage("A7", "C8"), ...
I am stuck on how to pass through two variables. This is where I have got so far, but I don't think I'm going in the right direction:
Sub main()
Dim rng As Range
Dim rng2 As Range
Dim aSht As Worksheet
Dim bSht As Worksheet
Set aSht = ThisWorkbook.Sheets("Sheet1")
Set bSht = ThisWorkbook.Sheets("Sheet2")
For Each rng In aSht.Range("A5:A8")
For Each rng2 In bSht.Range("C6:C9")
CreatePage(rng,rng2)
Next rng, rng2
End Sub
I have adjusted CreatePage() to hold two variables. I'm puzzled on getting the second variable in the for loop. Can anyone help out?
A nested for loop won't help here. You need to loop one range only and pick the required cell from the other
Sub main()
Dim rng As Range
Dim rng2 As Range
Dim aSht As Worksheet
Dim bSht As Worksheet
Set aSht = ThisWorkbook.Sheets("Sheet1")
Set bSht = ThisWorkbook.Sheets("Sheet2")
Dim idx As Long
Set rng = aSht.Range("A5:A8")
Set rng2 = bSht.Range("C6:C9")
'You might want to add code here to check the size and shape of your ranges
For idx = 1 To rng.Cells.Count
CreatePage rng.Cells(idx), rng2.Cells(idx)
Next
End Sub
Just a simple change needed.
For Each rng In aSht.Range("A5:A8")
For Each rng2 In bSht.Range("C6:C9")
CreatePage rng, rng2
Next rng2
Next rng

Apply formatting to a range using array loop - VBA

Using VBA i make to:
Set two ranges
Create an array of all ranges names
Loop the array in order to apply formatting to each range in the array
BUT on line .Interior.Color = vbRed
i receive the below error:
Run-time error '424': Object required
Code
Sub test()
Dim rng1 As Range, rng2 As Range
Dim strRanges As Variant
Dim i As Long
Set rng1 = Sheet1.Range("A1:D1")
Set rng2 = Sheet2.Range("C5:H5")
strRanges = Split("rng1,rng2", ",")
For i = LBound(strRanges) To UBound(strRanges)
With strRanges(i)
.Interior.Color = vbRed
End With
Next i
End Sub
i have already use:
With Range(strRanges(i))
instead of:
With strRanges(i)
without any luck!
Any help will appreciate.
You can do this in just one line if the ranges are in the same worksheet
Sheet1.Range("A1:D1,C5:H5").Interior.Color = vbRed
You can use union if the ranges are in the same worksheet
Dim rng1 As Range, rng2 As Range
Set rng1 = Sheet1.Range("A1:D1")
Set rng2 = Sheet1.Range("C5:H5")
Union(rng1, rng2).Interior.Color = vbRed
You can use real arrays for your ranges if they are in different worksheets
Sub test()
Dim rng(1 To 2) As Range
Set rng(1) = Sheet1.Range("A1:D1")
Set rng(2) = Sheet2.Range("C5:H5")
Dim i As Long
For i = LBound(rng) To UBound(rng)
With rng(i)
.Interior.Color = vbRed
End With
Next i
End Sub
If you don't have numbered range variable names then you can use another array:
Sub test()
Dim rngABC As Range, rngXYZ As Range
Set rngABC = Sheet1.Range("A1:D1")
Set rngXYZ = Sheet1.Range("C5:H5")
Dim ArrRng() As Variant
ArrRng = Array(rngABC, rngXYZ)
Dim rng As Variant
For Each rng In ArrRng
rng.Interior.Color = vbRed
Next rng
End Sub
Note that if you think you have to use numbered variable names like
Dim rng1 As Range, rng2 As Range
this always is a clear sign for using an array instead:
Dim rng(1 To 2) As Range
numbered variable names are a bad practice. Always choose meaningful names.

How to save data with formats and values in excel using VBA?

I am trying to save data from sheet("Billing") to another sheets("Bill_Register") with formats and values not formulas. Pasteformats and pastevalues are working separately not together. Please suggest me a better VBA. Thanks in advance.
Sub Save_invoice()
Dim i As Integer
Dim last As Long
Dim rng As Range, rng2 As Range
last = Sheets("Bill_Register").Range("A100000").End(xlUp).Row
Set rng = Sheets("Billing").Range("A1:J42")
Set rng2 = Sheets("Bill_Register").Range("A" & last + 2)
rng.Copy
rng2.PasteSpecial Paste:=xlPasteFormats
rng.Copy
rng2.PasteSpecial Paste:=xlPasteValues
End Sub
If you have merged cells and use xlPasteValues then an error will occur.
use variant array.
Sub Save_invoice()
Dim i As Integer
Dim last As Long
Dim rng As Range, rng2 As Range
Dim vDB
last = Sheets("Bill_Register").Range("A100000").End(xlUp).Row
Set rng = Sheets("Billing").Range("A1:J42")
vDB = rng '<~~ set array
Set rng2 = Sheets("Bill_Register").Range("A" & last + 2)
rng.Copy
rng2.PasteSpecial Paste:=xlPasteFormats
rng2.Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB '<~~ get value
End Sub
If rng and rng2 have the exactly same size (rows and columns) then you can use this to get the values:
rng2.Cells.Value = rng.Cells.Value
Some MCVE:
Public Sub TestMe()
Dim rng As Range
Dim rng2 As Range
Set rng = Range("A1:B2")
Set rng2 = Range("D2:E3")
rng2.Cells.Value = rng.Cells.Value
End Sub
If you are interested in the number formatting etc. but not necessarily the fill colour etc. then you could use:
Rng.Copy
rng2.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
There is no reason to copy twice, once it is in the clipboard it is there till replaced.
Paste the values first then the format:
Sub Save_invoice()
Dim i As Integer
Dim last As Long
Dim rng As Range, rng2 As Range
last = Sheets("Bill_Register").Range("A100000").End(xlUp).Row
Set rng = Sheets("Billing").Range("A1:J42")
Set rng2 = Sheets("Bill_Register").Range("A" & last + 2)
rng.Copy
rng2.PasteSpecial Paste:=xlPasteValues
rng2.PasteSpecial Paste:=xlPasteFormats
End Sub

Copy a selected range to another worksheet

I am using code below which I am trying to change so as not to use .select
Selection.Select ' from active worksheet
Selection.Copy
Sheets("Purch Req").Select
Range("A1").Select
ActiveSheet.Paste
I have tried using this but there is no output to the other worksheet.
Dim src2Range As Range, dest2Range As Range
Set src2Range = Selection 'source from selected range
Set dest2Range = Sheets("Purch Req").Range("A1").Resize(src2Range.Rows.Count, src2Range.Columns.Count) ' destination range _
'in Purch req worksheet
Here is good examples on How to avoid using Select in Excel VBA Link stackoverflow
Here is simples of
copy/paste - values = values - PasteSpecial method
Option Explicit
'// values between cell's
Sub PasteValues()
Dim Rng1 As Range
Dim Rng2 As Range
Set Rng1 = Range("A1")
Set Rng2 = Range("A2")
Rng2.Value = Rng1.Value
'or
[A2].Value = [A1].Value
'or
Range("A2").Value = Range("A1").Value
'or
Set Rng1 = Range("A1:A3")
Set Rng2 = Range("A1:A3")
Rng2("B1:B3").Value = Rng1("A1:A3").Value
'or
[B1:B3].Value = [A1:A3].Value
'// values between WorkSheets
Dim xlWs1 As Worksheet
Dim xlWs2 As Worksheet
Set xlWs1 = Worksheets("Sheet1")
Set Rng1 = xlWs1.Range("A1")
Set xlWs2 = Worksheets("Sheet2")
Set Rng2 = xlWs2.Range("A1")
Rng2.Value = Rng1.Value
'or
Set Rng1 = [=Sheet1!A1]
Set Rng2 = [=Sheet2!A1]
Rng2.Value = Rng1.Value
'or
[=Sheet2!A1].Value = [=Sheet1!A1].Value
'or
Worksheets("Sheet2").Range("A2").Value = Worksheets("Sheet1").Range("A1").Value
'// values between workbooks
Dim xlBk1 As Workbook
Dim xlBk2 As Workbook
Set xlBk1 = Workbooks("Book1.xlsm")
Set Rng1 = xlBk1.Worksheets("Sheet1").Range("A1")
Set xlBk2 = Workbooks("Book2.xlsm")
Set Rng2 = xlBk2.Worksheets("Sheet1").Range("A1")
Rng2.Value = Rng1.Value
'or
Set Rng1 = Evaluate("[Book1.xlsm]Sheet1!A1")
Set Rng2 = Evaluate("[Book2.xlsm]Sheet2!A1")
Rng2.Value = Rng1.Value
'or
Evaluate("[Book2.xlsm]Sheet2!A1").Value = Evaluate("[Book1.xlsm]Sheet1!A1")
'or
Workbooks("Book2.xlsx").Worksheets("Sheet1").Range("A1").Value = _
Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("A1").Value
End Sub
Simple copy/paste
Sub CopyRange()
Dim Rng1 As Range
Dim Rng2 As Range
Set Rng1 = Range("A1")
Set Rng2 = Range("A2")
Rng1.Copy Rng2
[A1].Copy [A2]
Range("A2").Copy Range("A1")
'// Range.Copy to other worksheets
Dim xlWs1 As Worksheet
Dim xlWs2 As Worksheet
Set xlWs1 = Worksheets("Sheet1")
Set Rng1 = xlWs1.Range("A1")
Set xlWs2 = Worksheets("Sheet2")
Set Rng2 = xlWs2.Range("A1")
Rng1.Copy Rng2
Set Rng1 = [=Sheet1!A1]
Set Rng2 = [=Sheet2!A1]
Rng1.Copy Rng2
[=Sheet1!A1].Copy [=Sheet2!A1]
Worksheets("Sheet1").Range("A1").Copy Worksheets("Sheet2").Range("A1")
''// Range.Copy to other workbooks
Dim xlBk1 As Workbook
Dim xlBk2 As Workbook
Set xlBk1 = Workbooks("Book1.xlsm")
Set Rng1 = xlBk1.Worksheets("Sheet1").Range("A1")
Set xlBk2 = Workbooks("Book2.xlsm")
Set Rng2 = xlBk2.Worksheets("Sheet2").Range("A2")
Rng1.Copy Rng2
Evaluate("[Book1.xlsm]Sheet1!A1").Copy Evaluate("[Book2.xlsm]Sheet2!A2")
Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("A1").Copy _
Workbooks("Book2.xlsx").Worksheets("Sheet1").Range("A1")
End Sub
PasteSpecial method
Sub PasteSpecial()
'Copy and PasteSpecial a Range
Range("A1").Copy
Range("A3").PasteSpecial Paste:=xlPasteFormats
'Copy and PasteSpecial a between worksheets
Worksheets("Sheet1").Range("A2").Copy
Worksheets("Sheet2").Range("A2").PasteSpecial Paste:=xlPasteFormulas
'Copy and PasteSpecial between workbooks
Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("A1").Copy
Workbooks("Book2.xlsx").Worksheets("Sheet1").Range("A1").PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
End Sub
There is many ways to do that, but here goes two.
1)
Sub pasteExcel()
Dim src2Range As Range
Dim dest2Range As Range
Dim r 'to store the last row
Dim c 'to store the las column
Set src2Range = Selection 'source from selected range
r = Range("A1").End(xlDown).Row 'Get the last row from A1 to down
c = Range("A1").End(xlToRight).Column 'Get the last Column from A1 to right
Set dest2Range = Range(Cells(1, 1), Cells(r, c))
dest2Range.PasteSpecial xlPasteAll
Application.CutCopyMode = False 'Always use the sentence.
End Sub
2)
Sub pasteExcel2()
Dim sht1 As Worksheet
Dim sht2 As Worksheet 'not used!
Dim src2Range As Range
Dim dest2Range As Range
Dim r 'to store the last row
Dim c 'to store the las column
Set sht1 = Sheets("Sheet1")
Set sht2 = Sheets("Sheet2")
sht1.Activate 'Just in case... but not necesary
r = Range("A1").End(xlDown).Row 'Get the last row from A1 to down
c = Range("A1").End(xlToRight).Column 'Get the last Column from A1 to right
Set src2Range = Range(Cells(1, 1), Cells(r, c)) 'source from selected range
Set dest2Range = Range(Cells(1, 1), Cells(r, c))
sht2.Range(dest2Range.Address).Value = src2Range.Value 'the same range in the other sheet.
End Sub
Tell me if you need some improvement.

Resources