So I am trying to have a cell reference in my code be an indirect reference. For instance I want to update the value in column B cell "X" where X is defined in cell B1.
Here is the code that I am currently trying but I keep getting an out of range exception. I am very new to VBA so my syntax could just be very far off.
Workbooks("Personal_Finances").Sheets(categoryType).Range("$B($B$1)").Value = ammount
Try,
with Workbooks("Personal_Finances").Sheets(categoryType)
.cells(.Range("B1").Value, "B") = ammount
'alternate
.Range("B" & .Range("B1").Value) = ammount
end with
Here .Range("B1").Value is used for the row reference in .Cells. The alternate is closer to what you were originally attempting.
I've wrapped the working code in a With ... End With block to maintain the parent worksheet reference.
There is no need for $ in a quoted string cell reference unless used in a formula populating multiple cells at once.
def a new variable and assign the value of cell $b$1 to it.
dim temp_row as integer
temp_row.value =Workbooks("Personal_Finances").Sheets("categoryType").Range("B1").Value
Workbooks("Personal_Finances").Sheets(categoryType).Range("$B" & temp_row).Value = amount
or just do the same thing in one line.
Or:
With Workbooks("Personal_Finances").Sheets(categoryType).Range("B1")
.Offset(.Value2 - 1) = ammount
End With
Where the “With ... End With” block references cell B1 of wanted worksheet in wanted workbook and the nested statement offsets it by its value minus one (to reach wanted row)
Related
The current code is as follows:
With Sheets(sheetChk)
a = .Range(chkCell, .Range(Left(chkCell, 1) & Rows.Count).End(xlUp)).Value
End With
sheetChk is defined as the desired sheet index.
chkCell is defined as the desired "first cell" in the range.
I am attempting to assign a range to a. The problem is that the above code grabs the last cell in the column, entirely. Rather, I want to end at a specific row as I have other data in the same column after the desired range that needs to be ignored. How can I accomplish this?
First to assign a range, you need to have a Set first.
Example: Set a = Range("A1")
Another problem I see is that you're putting a .value at the end. That doesn't make sense for a range.
If you want to specify a specific row, then you need to include that in the code instead of using end(xlUp).
Example if it's row 50:
With Sheets(sheetChk)
Set a = .Range(chkCell, .Range(Left(chkCell, 1) & 50).End(xlUp))
End With
What you're currently using is finding the bottom row being used, which doesn't sound like you want. If you want to go the other direction (i.e. from the starting cell down until there's an empty cell), you can use this code:
With Sheets(sheetChk)
Set a = .Range(chkCell, .Range(Left(chkCell, 1) & chkCell.Row).End(xlDown))
End With
Based on your code, it looks like you might be putting an address in whatever cell chkCell is. If you have the row in that cell, and assuming you never exceed column z, then you could use this code to find the row:
With Sheets(sheetChk)
Set a = .Range(chkCell, .Range(Left(chkCell, 1) & Right(chkCell,Len(chkCell)-1))
End With
If that doesn't work, you need to figure out some method determine what row to use. Hope that helps.
dim rng as range
set rng = thisworkbook.sheets("sheet1").range("a1:a666")
I have the following formula:
dim functionString as String
functionString = "IFERROR(AND(MATCH(" & FirstName.Value & ",C2:C1048576, 0)>0, (MATCH(" & LastName.Value & ",B2:B1048576, 0)>0)), MAX(A2:A1048576)+1)"
What I want to be able to do is call it from the VBA code so it would look like.
application.WorksheetFunction(functionString)
I know that I can place it on the worksheet at some cell that's never going to be used: IE:
Activesheet.range("ZZ1000").formula = "="& functionString
and then reference that cell without worrying whether the program would inadvertently crash; but is there a way to do such a formula from VBA directly?
Basically I'm looking to see whether FirstName.Value and LastName.Value (which are defined elsewhere in the code) together are in the worksheet in column B and column C. As I'm writing this, I realized I need to make sure that they are both in the same row as well and not in different rows.
You could try Application.Evaluate(functionString) but depending on complexity it may be better to use VBA functions instead of WorksheetFunctions.
The Application.Match function will return an error type if the value is not found in the range/array, so we Dim first, last as variant type to allow for this (without raising an error).
Dim first, last
' find the row where FirstName.Value appears in column C
first = Application.Match(FirstName.Value, Columns(3), False))
' find the row where LastName.Value appears in column B
last = Application.Match(LastName.Value, Columns(2), False))
If Not IsError(first) And Not IsError(last) Then
If first = last Then
' match was found in both columns and on same row
' do something else...
End If
End If
Check This
I need a help. I want to copy whole cell from A sheet name "Components" only if value in Column C is > 0 to a new Sheet name "Load list"
Can someone please give me the macro code for this?
on your new sheet you can add this condition the cell or range of cells:
=IF(Components!C5>0,Components!A5)
where C5 has thevalue to compare, and A5 has the value copy if the condition happens.
Right in my swing!
The formula given by #sweetkaos will work fine, in case you want to replicate the data as it is with blanks where data is not found.
I will imagine a slightly more complicated situation. I am assuming you want just one line in the next format as is shown in your image.
Also conveniently assuming the following:
a. both sheets have fixed start points for the lists
b. 2 column lists - to be copied and pasted, with second column having value
c. Continuous, without break source list
d. basic knowledge of vba, so you can restructure the code
Here is the code. Do try to understand it line by line. Happy Excelling!
Sub populateLoadList()
'declaring range type variables
Dim rngStartFirstList As Range, rngStartLoadList As Range
'setting values to the range variables
'you must change the names of the sheets and A1 to the correct starts of your two lists
Set rngStartFirstList = Worksheets("Name_of_your_fist_sheet").Range("A1")
Set rngStartLoadList = Worksheets("Name_of_your_second_sheet").Range("A1")
Do While rngStartFirstList.Value <> ""
If rngStartFirstList.Offset(1, 0).Value < 0 Then
Range(rngStartFirstList, rngStartFirstList.Offset(0, 1)).Copy
rngStartLoadList.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Set rngStartLoadList = rngStartLoadList.Offset(1, 0)
End If
Set rngStartFirstList = rngStartFirstList.Offset(1, 0)
Loop
End Sub
Basically what i want is ... if Value on C is >0 i want whole column 10 copied to that new sheet .... not only that cell
I've a sheet "Employee_log" which contains the details of everyday log in a single string in column A. For example:
A1 : EMP1~EMP2~JOB1~JOB2...
A2 : EMP1~EMP3~JOB1~JOB3...
A3 : EMP2~EMP3~JOB2~JOB3...
Now I want the number of occurrences of EMP1 and JOB1 from this column and populate into another sheet "Ind_Detail":
I'm using, but it's not working properly,:
Set DSht = Worksheets("Employee_log")
Dsht. Activate
ThisWorkbook.Sheets("Ind_Detail").Cells(1, "A") =CountIfs(Range("A:A"), _
"EMP1", Range("A:A"), "JOB1")
Set DSht = Nothing
You need to wrap the criteria in wildcard characters to count if a 'string in a string' occurs.
with Worksheets("Employee_log")
ThisWorkbook.Sheets("Ind_Detail").Cells(1, "A") = _
application.CountIfs(.columns(1), "*EMP1*", .columns(1), "*JOB1*")
end with
I've also blocked the worksheet into a With ... End With statement so that the ranges (e.g. .Columns(1) in the sample) could be localized to the Employee_log worksheet. Your sample of Range("A:A") left the definition of the parent worksheet ambiguous.
See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.
CountIfs is a WorkSheet function, it cannot be used directly in VBA. Either write your formula directly in the destination cell:
=CountIfs(Employee_log!A:A, "*EMP1*", Employee_log!A:A, "*JOB1*")
or, in VBA, use the WorksheetFunction object to access Worksheet functions:
ThisWorkbook.Sheets("Ind_Detail").Cells(1, "A") =WorkSheetFunction.CountIfs(Range("A:A"), "*EMP1*", Range("A:A"), "*JOB1*")
EDIT: added wildcards as in Jeeped's answer
I have an XL file with some data to be manipulated. I think I will need to use a VB script to do this - but perhaps there is a simpler way with a formula. Just the same, could someone point out BOTH ways of achieving the following?
I have a column of numeric values (ID) in Sheet 1.
I want to use each ID as an index to lookup a list in Sheet 2.
Sheet 2 has two columns
First column is the index and Second column is the Text String
e.g.
1 Apple
2 Orange
3 Pear
What I want is to replace the column of IDs in sheet 1 with the looked up text string from Sheet 2!
Thats all...
Please help!
Not a tough situation there. Here are some solutions...
With VBA:
I know you said you're a little new with VB so I tried to explain each line as I went along. Also, the code is free-handed so forgive me if I left an error in there somewhere.
Sub replaceData()
dim i as integer, j as integer 'These are just some variables we'll use later.
dim sheetOne as worksheet, sheetTwo as worksheet, myWb as workbook
dim myData as string, myId as string
set myWB = excel.activeworkbook 'These three lines set your workbook/sheet variables.
set sheetOne = myWB.worksheets("Old Data")
set sheetTwo = myWB.worksheets("New Data")
for i = 1 to sheetTwo.usedrange.rows.count 'This loops through the rows on your second sheet.
myId = sheetTwo.cells(i,1).value 'This assigns the value for your id and the data on your second sheet.
myData = sheetTwo.cells(i,2).value
for j = 1 to sheetOne.usedrange.rows.count 'This loops through the rows on your first sheet.
if sheetOne.cells(j,1).value = myId then 'This checks each row for a matching id value.
sheetOne.cells(j,1).value = myData 'This replaces that id with the data we got from the second sheet.
end if
next j
next i
end sub
With an Excel formula:
Place the following formula in cell C1 of the first worksheet (the
sheet with the IDs you will be replacing). **Note that you will
have to replace the "InsertSheetTwoNameHere" portion with the name
of your second sheet (don't remove those single quotes though). Also
note you will need to replace the "1000" with the number of the last
used row in sheet two.
=vlookup(A1,’InsertSheetTwoNameHere’!$A$1:$B$1000,2,FALSE)
Next simply drag the handle on the cell that makes it copy itself
(whatever the heck it's called) all the way down to the end of your
range.
Next, copy those cells and then paste them over the IDs using the
Values Only setting.
Hope this helps and good luck.