I have a vector of values (C8:AM8) in an excel worksheet (XYZ). Using VBA code, what I need to do is copy and paste those cells starting at C11, BUT if C11 has a value, then I need to offset by 1 row and paste the above values (C8:AM8) in C12, or if C12 has a value, then the next empty row.
Basically, as of now, I have something that looks like this....
Dim CellName As String
Dim CheckCell As Range
Dim RowA As Range
CellName = "C8:AM8"
For Each CheckCell In Sheets("XYZ").Range("C11:C50")
If Not IsEmpty(CheckCell.Value) Then
Set RowC = CheckCell.Offset(1, 0)
Else
Range(CellName).Copy Range("C11" + RowC)
End If
Next CheckCell
Minimally, I know that the issue is here:
Range(CellName).Copy Range("C11" + RowC)
I am new to VBA and so I'm not sure of the proper syntax to use. I've tried multiple permutations with the RowC inside the parentheses, outside the parentheses, in quotes, outside of quotes, etc. and I can't seem to nail it. I think this issue is very simple, but I am clearly missing something critical and can't seem to find what I need online.
Any help would be much appreciated.
Thank you!
I'd go with:
With Sheets("XYZ")
.Range("C8:AM8").Copy .Range("C" & WorksheetFunction.Max(11, .Cells(.Rows.count, "C").End(xlUp).Offset(1).Row))
End With
The following line copies your range after the last non-empty cell in row C:
Worksheets("XYZ").Range("C8:AM8").Copy Worksheets("XYZ").Range("C1000000").End(xlUp).Offset(1)
You need to put this line of code:
Range(CellName).Copy Range("C11" + RowC)
Like this:
Dim my_row as integer
my_row=11
Range(CellName).Copy Range("C" & (my_row + RowC))
Related
I know this is as trivial and easy as it gets, but I can't figure it out for the life of me. I am completely clueless when it comes to excel formulas.
[2
I would like the cell U7 to increment by 1 for each "D" it finds in the selected range of cells (E7:S7), same with the cell V7, increment by 1 but for each "N" it finds in the same range of cells.
I was trying to use multiple if statements like:
=IF(E7:S7="D";1;0)
But that doesn't seem to work at all. Any help would be greatly appreciated.
This is what you are after. Use COUNTIF to count the number of times D or N occur in your range.
=COUNTIF(E7:S7,"D")
You want to use the COUNTIF()-Function:
=COUNTIF(E7:S7, "D")
For more reference, see the Microsoft Office support site.
You can use a =COUNTIF() Function.
You referred to VBA, so I assume you want to run this in a program possibly.
The code below will run through the used rows starting in row 7 and then insert the number of 'D" letters seen in the columns E to S.
Sub CountNumberOfDLetters()
Dim LastRow As Long
With ActiveSheet.UsedRange
LastRow = .Rows(.Rows.Count).Row
End With
Dim RowReference As Long
Dim DataRange As Range
Dim NumberOfD As Double
For RowReference = 7 To LastRow
Set DataRange = Range(Cells(RowReference, "E"), Cells(RowReference, "S"))
NumberOfD = WorksheetFunction.CountIf(DataRange, "D")
Cells(RowReference, "U").Value = NumberOfD
Next RowReference
End Sub
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,"")
I am very new to VBA and only understand basic principles so this might not be possible with the code I have used. I have some code for finding a value in Sheet1 using a range from Sheet2, which pastes entiore row to Sheet3. How could I modify it so that it will still paste the row based on a like value and not an exact match, so would paste the row if "Company Ltd" was in Sheet1 and just Company was in the range. I have tried wildcard statements but can't get them to work with a range. Can anyone point me in the right direction?
Option Compare Text
Sub Find_Values()
Dim c1 As Range, rng1
Dim c2 As Range, rng2
Dim lastrow As Long
Set rng1 = Range("sheet1!a1:a10")
Set rng2 = Range("sheet2!a1:a10")
For Each c2 In rng2
For Each c1 In rng1
If c1 = c2 Then
c1.EntireRow.Copy
Sheets("sheet3").Activate
lastrow = Cells(Rows.Count, "a").End(xlUp).Row
Range("a" & lastrow + 1).Select
ActiveSheet.Paste
End If
Next c1
Next c2
End Sub
Thank you
What Sam was trying to say is modify this line:
If c1 = c2 Then
You can use:
If InStr(c1,c2) > 0 Then '...
You can also use
If InStr(c1,c2) > 0 or InStr(c2,c1) > 0 or UCase(c2) = UCase(c1) Then '...
Etc.
There are a lot of different comparisons you can add to this line of code.
Making both values uppercase is a good way to do comparisons if you are just interested in the value.
You can also change your code (quite significantly) to use the find keyword which allows you to use wildcards and search using xlWhole and xlPart
It really depends on what the differences are and how much code you want to change.
You might also be interested in using the Like Operator, although if you're simply comparing values, it is probably not what you need.
To find if a string exists within another string, use the InStr function. For example,
Dim stringPosition As Integer
stringPosition = InStr("Company Ltd", "Company")
If stringPosition > 0 Then
' we found a match
End If
The variable tablelength counts how many items are in a table of mine. I want to select my entire table, but it varies in sizes so my range has to include a variable. I've googled a lot and searched this site (Using variables in Excel range <- that method looked promising but didn't work). Below is a snippet of my code, but includes everything that is relevant.
Private Sub CommandButton1_Click()
Dim shSource As Worksheet
Dim shDest As Worksheet
Dim tablelength As Integer
Set shDest = ThisWorkbook.Sheets("Sheet2")
'here comes some code that determines the value of tablelength, which is 8 in this case
shDest.Range("L" & "4" & ":" & "M" & tablelength).Select
End Sub
I appreciate the help.
edit: the debugger highlights the shDest.Range code.
Unless you need tablelength variable somewhere else in the code, you could try using:
shDest.Range("L4").CurrentRegion.Select
CurrentRegion.Select will select all cells starting from "L4" until it reaches a blank row and column, so providing your tables are surrounded by blank cells this should select the whole table regardless of the size
Here you go, try this:
ActiveSheet.Range(Cells(2, 3), Cells(10, 4)).Select
Taken from http://support.microsoft.com/kb/291308
The first parameter to Cells is the row and the second is the column as a number.
So for you it would look something like this:
shDest.Range(Cells(4, 12), Cells(tablelength, 13)).Select
If it's a proper Table on the spreadsheet, and not just cells formatted to look like a table, you can directly refer to the 'live' size of the table in your code without jumping through all these hoops.
In your VBA code,
The 'Table' is referred to as a ListObject
You can declare a new ListObject, and look up its DataBodyRange.Rows.Count
This should work:
Sub MyMacro()
Dim Tabl As ListObject
Set Tabl = Worksheets("Sheet1").ListObjects("Table1")
MsgBox Tabl.DataBodyRange.Rows.Count
End Sub
You can also set a range variable to refer to the 'Data' range. You need to use the following code.
Dim Rng As Range
Set Rng = Worksheets("Sheet1").ListObjects("Table1").DataBodyRange
Now Rng.Cell(1,1) or Rng.Range("A1") refers to the top left cell of the data body and so on and so forth...
I need to write macro or function or whatever in VBA, what will apply a function with arguments (e.g. vlookup) on a rangeof cells. I thought it can be done by macro, but there I could not use arguments for vlookup, so I dont know any way how to do it. Is it possible?
For example:
I want to have this:
vlookup(A1;G1:H50;2;0) in cell B1 and
vlookup(A2;G1:H50;2;0) in cell B2 and so on, to e.g. B10
but I want to write formula only once and let other cells to be filled automaticaly.
Thanks a lot.
If I understand you correctly, here is one way (you'll have to change the delimiter to match your country settings, and adjust where you want the formula to go, I just put in B1:B10 as an example):
Sheet1.Range("B1:B10").Formula = "=vlookup(A1,$G$1:$H$50,2,0)"
This bit of code will write the formula to the range B1:B10 on sheet1, which has the same effect of putting the formula in the B1 and then "dragging" it down to B10. What makes this work is that Excel has the built in functionality of auto-incrementing a formula references based on whether or not the range is preceded by a $ symbol.
If a column reference has a $ in front, it will not increment as the formula is dragged across columns. If the $ is in front of the row reference, it will not increment as the formula is dragged down.
So looking back at my proposed formula, you can see that the A1 will increment as the formula is dragged to B2, B3, B4, etc...auto-incrementing the look up value to be cell A2, A3, A4, respectively. The look up range does not change at all because both the column and the row references are preceded by a $.
If you run the code I gave you, you'll see that you should have the expected results of only writing one formula, but changing the look up range through the built in auto incrementing functionality.
--------------------More edits based on comments--------------------
To do what you want, you don't need VBA at all (even though you initially requested a VBA / macro solution). You can put the formula in a cell and drag it down to how ever far down you want it to go. Please take a look at this link to see if it helps answer your questions:
How to fill data automatically in Excel
You could use Application.VLookup just like this following this example http://www.exceltrick.com/formulas_macros/vlookup-in-vba/:
Sub SetValues(columnToChange As String, columnToLookup As String, range As String, startColumn As Integer, endColumn As Integer)
For number = startColumn To endColumn Step 1
valueLookup = columnToLookup + CStr(number)
valueToChange = columnToChange + CStr(number)
Sheets("yourSheetName").Range(valueToChange).value = Application.VLookup(valueLookup, range, 2, 0)
Next number
End Sub
If you want to call them, create another subroutine without parameters that you can call from a button click for instance.
Sub DoStuff()
On Error GoTo ErrorHandler
Dim valueLookup as String
Dim valueToChange as String
Dim range as String
Dim firstColumn as Integer
Dim lastColumn as Integer
Label1:
valueLookup = InputBox("Enter the column to lookup")
valueToChange = InputBox("Enter the column to change")
range = InputBox("Enter the range of the lookup")
firstColumn = CInt(InputBox("Enter the first column number to lookup"))
lastColumn = CInt(InputBox("Enter the last column number to lookup"))
Call SetValues(valueToChange, valueLookup, range, firstColumn, lastColumn)
Exit Sub
ErrorHandler:
MsgBox("One value has an error in it.")
Resume Label1:
End Sub