This question already has answers here:
Excel VBA, getting range from an inactive sheet
(3 answers)
Closed 7 years ago.
It is in the same workbook. I have a few numbers in a range in Sheet3 and the button is in Sheet2. I want it to count the number of rows and then return the value to me in a msgbox. If I run this with the Sheet3 open it runs fine. If I run it with a different sheet active I get an object error for the counting part. It does insert the 3 in cell A2 no matter what sheet is active.
Sub problem3run()
Dim rngtest As Range
Set rngtest = Sheet3.Range("A2")
rngtest.Value = 3
Dim rngacount As Integer
rngacount = Sheet3.Range(Range("A4"), Range("A4").End(xlDown)).Rows.Count
MsgBox (rngacount)
End Sub
What is messed up in this?
Two issues here. The first is that data type Integer can't hold all the rows in certain excel files, so you'll want to use Long instead. The second is that you are defining rngacount with unqualified range objects. Try this:
Sub problem3run()
Dim rngtest As Range
Set rngtest = Sheet3.Range("A2")
rngtest.Value = 3
Dim rngacount As Long '<~ use Long here rather than Integer
With Sheet3 '<~ use a With here to qualify ranges
rngacount = .Range(.Range("A4"), .Range("A4").End(xlDown)).Rows.Count
End With
MsgBox (rngacount)
End Sub
Related
I have 3 worksheets: WUR, Homologation and read.
I want to read column 1 from "WUR", when i find first value i want to compare with first column from "Homologation", if the values is the same then put this values in firm column in worksheet 3, "Read". This code is from Button found in Worksheet 3.
I try this:
Sub Read_Click()
Dim x As Integer
Application.ScreenUpdating = False
NumRows = ActiveWorkbook.Worksheets("WUR").Range("A1", ActiveWorkbook.Worksheets("Homologation").Range("A1").End(xlDown)).Rows.Count
Range("A1").Select
For x = 1 To NumRows
Next
End Sub
With my code i try to find first values from the first column in "WUR" but my code work in Sheet 3, not in first WorkSheet where i want.
It might be because in
Range("A1").Select
you don't specify the worksheet where A1 has to be selected. If you therefore have currently another worksheet open, the selection will be made there. Try defining it like you did the line above
ActiveWorkbook.Worksheets("WUR").Range("A1").Select
This question already has answers here:
Excel VBA, getting range from an inactive sheet
(3 answers)
Closed 6 years ago.
I am using VBA to loop through a list of reference numbers ROID and return the correct full name (ROIDA) from a separate page. The page where the full name is located has the reference number in column D and the full name in column A.
Sub Main()
'Set variable types
Dim WorksheetA As Excel.Worksheet
Dim WorksheetB As Excel.Worksheet
Dim ROID As Range, ROIDA As Range
Set WorksheetA = ActiveWorkbook.Sheets("WorksheetA")
Set WorksheetB = ActiveWorkbook.Sheets("Approval Flows")
'Replacing ROID #s with full Name
'Define range of active requesting offices
Set ROID = WorksheetA.Range(Range("A7"), Range("A7").End(xlDown))
'Define range of attention lines and associated ROIDs
Set ROIDA = WorksheetB.Range(Range("D7"), Range("D7").End(xlDown))
'Loop through ROIDs and replace with ATTN line
For Each ID In ROID
Set Match = ROIDA.Find(ID)
If Not Match Is Nothing Then
ID = Match.Offset(0, -3)
End If
Next ID
End Sub
When I try to run the script, I receive an objected oriented error from this line:
Set ROIDA = WorksheetB.Range(Range("D7"), Range("D7").End(xlDown))
Is this because I'm working with multiple sheets? I am trying hard not to use the activate or select functions.
Yes, if A/B are not active when you run the macro. You need to fully qualify all ranges with sheets. Also better to go the end and work up than go Down in case you have nothing after A7.
With WorksheetA
Set ROID = .Range(.Range("A7"), .Range("A7").End(xlDown))
'Or Set ROID = .Range(.Range("A7"), .Range("A" & Rows.count).End(xlup))
End With
'Define range of attention lines and associated ROIDs
With WorksheetB
Set ROIDA = .Range(.Range("D7"), .Range("D7").End(xlDown))
End With
Column 1 is in Sheet1 and column 2 is in Sheet2. If the value is not found , then highlight that cell. I am trying to do a vlookup comparing two columns. I think the Syntax is incorrect. Please see my code I was trying below:
Option Explicit
Sub VlookupColoums()
' declarations
Dim lookFor As Range
Dim srchRange As Range
Dim I As Long
Dim vtest As Variant
' start
Set lookFor = Sheets("Sheet1").Range("A13").End(xlUp)
Set srchRange = Sheets("Sheet2").Range("A2").End(xlUp)
vtest = Application.VLookup(lookFor.Rows.Count, srchRange.Rows.Count, 2, False)
' process
For I = 1 To lookFor.Rows.Count
If IsError(vtest) Then
srchRange.Interior.Color = 4
Else
Exit Sub
End If
Next I
End Sub
Assuming you have data on Sheet1!A1:A15 and Sheet2!A1:A10.
Also assuming you want to highlight unique cells (ones withouth at least one identical in the other list) on Sheet2.
Basically you want to format all the cells that if counted on the other list comes up with 0. The steps:
Select all the cells to be evaluated on Sheet2
Go to Home/Styles/Conditional Formatting
Select New Rule, then Use a formula to determine...
Enter this formula: =COUNTIF(Sheet1!$A$1:$A$5,A1)=0
Click on the Format button, and set up a formatting for the unique cells
OK
Profit. :)
I have a workbook with "Results" being sheet 3, this being the worksheet I want to use.
I have tried a few formulaes to try and add a macro to do the following:
I have column G with percentages. I then have column I where I would like there to be a result saying TRUE/FALSE where the contents of G are equal to 100%. Column G is formatted to percentage with two decimals.
Some considerations: I have my first row being a Hyperlink to another sheet, then my headings, then the first row of "results". I have 457 rows, if there is a measurement of the range, perhaps it could be on A?
I keep getting this error 9 with my range and have got a bit stuck.
Thanks in advance!
Sub PartialHits1()
Dim rng As Range
Dim lastRow As Long
Dim cell As Range
With Sheet3
lastRow = .Range("G" & .Rows.Count).End(xlUp).Row
Set rng = .Range("G1:G" & lastRow)
For Each cell In rng
If cell.Value = 100
Then
cell.Range("I1:I1").Value = 100
End If
Next
End With
End Sub
(I have hacked this a bit, just was trying to get it to set as 100 instead of the TRUE/FALSE Also was playing around near the Sheet 3 part as I got errors.)
RangeVariable.Range can refer only to a cell within RangeVariable, so you can't refer to column I in this way. Try: .Range("I"&cell.row)=100.
Also your criteria is probably wrong, if you have 100% in a cell it's actual value is 1.
And last question: why do you want to do this with VBA, it would be much more simple with worksheet function =IF(G3=1,100,"")
Could someone please help me with some VBA code.
I am trying to copy 2 ranges of cells between workbooks (both workbooks should be created beforehand as i don't want the code to create a new workbook on the fly).
Firstly I need to copy these ranges-
From 'Sheet 3' of booka.xls, Range: Cell H5 to the last row in column H with data
copy this to 'Sheet 1' of bookb.xls, starting in Cell B2 for as many cells down in the B column
Secondly I need to copy these ranges-
From 'Sheet 3' of booka.xls, Range: Cell K5 to the last row in column K with data
copy this to 'Sheet 1' of bookb.xls, starting in Cell D2 for as many cells down in the D column
Here is what I have so far:
Sub CopyDataBetweenBooks()
Dim iRow As Long
Dim wksFr As Worksheet
Dim wksTo As Worksheet
wksFr = "C:\booka.xls"
wksTo = "C:\bookb.xls"
Set wksFrom = Workbooks(wksFr).Worksheets("Sheet 3")
Set wksTo = Workbooks(wksTo).Worksheets("Sheet 1")
With wksFrom
For iRow = 1 To 100
.Range(.Cells(iRow, 8), .Cells(iRow, 9)).Copy wksTo.Cells(iRow, 8)
Next iRow
End With
End Sub
Assuming you have the reference to wksFrom and wksTo, here is what the code should be
wksFrom.Range(wksFrom.Range("H5"), wksFrom.Range("H5").End(xlDown)).Copy wksTo.Range("B2")
wksFrom.Range(wksFrom.Range("K5"), wksFrom.Range("K5").End(xlDown)).Copy wksTo.Range("D2")
Here's an example of how to do one of the columns:
Option Explicit
Sub CopyCells()
Dim wkbkorigin As Workbook
Dim wkbkdestination As Workbook
Dim originsheet As Worksheet
Dim destsheet As Worksheet
Dim lastrow As Integer
Set wkbkorigin = Workbooks.Open("booka.xlsm")
Set wkbkdestination = Workbooks.Open("bookb.xlsm")
Set originsheet = wkbkorigin.Worksheets("Sheet3")
Set destsheet = wkbkdestination.Worksheets("Sheet1")
lastrow = originsheet.Range("H5").End(xlDown).Row
originsheet.Range("H5:H" & lastrow).Copy 'I corrected the ranges, as I had the src
destsheet.Range("B2:B" & (2 + lastrow)).PasteSpecial 'and destination ranges reversed
End Sub
As you have stated in the comments, this code above will not work for ranges with spaces, so substitute in the code below for the lastrow line:
lastrow = originsheet.range("H65536").End(xlUp).Row
Now ideally, you could make this into a subroutine that took in an origin workbook name, worksheet name/number, and range, as well as a destination workbook name, worksheet name/number, and range. Then you wouldn't have to repeat some of the code.
You can use special cells like Jonsca has suggested. However, I usually just loop through the cells. I find it gives me more control over what exactly I am copying. There is a very small effect on performance. However, I feel that in the office place, making sure the data is accurate and complete is the priority. I wrote a response to a question similar to this one that can be found here:
StackOverflow - Copying Cells in VBA for Beginners
There is also a small demonstration by iDevelop on how to use special cells for the same purpose. I think that it will help you. Good luck!
Update
In response to...
good start but it doesn't copy anything after the first blank cell – trunks Jun 9 '11 at 5:08
I just wanted to add that the tutorial in the link above will address the issue brought up in your comment. Instead of using the .End(xlDown) method, loop through the cells until you reach the last row, which you retrieve using .UsedRange.Rows.Count.