Issue using Cells().formula - vba

I'm attempting to input a formula into a column in a table. The formula uses the table headers to solve for a result. the location used in the Cells() command also uses a variable as seen below:
Sheet6.Cells(17, k + 7).Formula = "=[#[Net Weight]]/([Length] * $J$10 * $J$10)"
Range(Cells(17, k + 7).Address()).Select

Your code works for me, if I make sure that the cell is in the table or next to it. If I put it in a random cell it results in the same error as you get.

Related

I am applying Max formula of excel using OpenPyXL but it is deleting formula on opening file

Please note that I am applying MAX formula in a column through OpenPyXL.
The source data is also coming from Formula applied through OpenPyXL.
Problem is that it is deleting formula.
Some code given below:
#cumm profit
if(wsprofitrowno==2):
wsprofit.cell(row=wsprofitrowno, column = 3).value = '=SUM(B2)'
else:
wsprofit.cell(row=wsprofitrowno, column = 3).value = '=B%d+C%d' % (wsprofitrowno, wsprofitrowno-1)
#Peak
if(wsprofitrowno>2):
wsprofit.cell(row=wsprofitrowno, column = 4).value = '=MAX($C$2:C%d' % (wsprofitrowno)
wb.save("output.xlsx")
wsprofitrowno = wsprofitrowno + 1
You are trying to add this as a formula in the Excel cell '=MAX($C$2:C%d' % (wsprofitrowno) so Excel would see that as an error and the formula would be rejected. The Excel MAX function needs a closing bracket, since you are trying to use the row 'wsprofitrowno' as the row value for the second coordinate of the MAX formula you can do either
Add the close bracket; '=MAX($C$2:C%d)' % wsprofitrowno
just do '=MAX($C$2:C' + str(wsprofitrowno) + ')'

Using variable sheetname in VBA

I'm a novice at VBA (just starting to learn). In my userform I have a combobox with values from 1 to 12(not string) representing the months. I want the user to pick a month and based on that, the multiple listboxes and labels I have placed should get filled by the relevant values in one of the 12 sheets representing each month. as I am a novice I have a lot of problems here but for starters the following lines do not seem to work on userform_initiate()
For j = 0 To 1
arr_trh(0, j) = Sheets("Sheet6").Cells(4, j + 1)
Next j
I can get it to work for a single sheet by using
arr_trh(0, j) = Sheet6.Cells(4, j + 1)
However, what I'm trying to do later on is to create a string and somehow concatenate "Sheet" and combobox value to pass on to Sheets() function.
Any help would be appreciated
Thanks
Rather than referring to the Sheet object like:
v = Sheet1.Range("A1")
use:
v = Sheets(1).Range("A1")
which you can index like:
v = Sheets(i).Range("A1")
where i is a variable.
I guess, my this answer will help you to figure out how to refer to sheets. Also, it tells about caveats of Index property.

Defining a group of cell names using vba

I have a question, I am trying to define a cell name in excel using vba. Now to select one cell only i have been using the following code which proves to work fine:
Range("A1").Name = "zm_1"
However I need to name a number of cells in a column i.e:
Range("A1").Name = "zm_1"
Range("A2").Name = "zm_2"
Range("A3").Name = "zm_3"
Range("A4").Name = "zm_4"
Since this is quite tedious for 100 cells, i have been trying to use an array:
For i=1 to 100
Range("A(i)").Name = "zm_(i)"
next
^ this however gives an error which i have been unable to track. Any ideas/suggestions on how can it be done? Thanks!
The problem is with Range("A(i)").Name = "zm_(i)" The " indicate that something is text, so you should place your (i) out of the ".
For the first part it is quiet easy, as you can also use the row and column way of describing the cell. Therefore that becomes
Range(Cells(i,1)).Name
For the second part you need to concatenate the text and the numbers. That becomes: "zm_" & i
You don't need an array, just your loop:
For i = 1 to 100
Range("A" & i).Name = "zm_" & i
next
Try the below code and hope it helps you:
For i = 1 To 100
ThisWorkbook.Sheets(1).Range("A" & i).Name = "zm_" & i
Next
Where Sheet(1) refers to Sheet1 of the current workbook.

Paste to next empty column

I already have this code that paste to the next empty row. It works perfectly but now I have a new VBA project that i would like to do the same thing but columns instead of rows
Current Code:
intLast = shtAlpha.Cells(Rows.Count, "C").End(xlUp)
intNext = intLast + 5 - (intLast + 1) Mod 5
Set rngDest = shtAlpha.Cells(intNext, "C").Resize(5, 3)
I tried this but I got Compile Error Method or data member not found, It highlights
.Cells
/
intLast = Omega.Cells(Columns.Count, "1").End(xlRight).Column
intNext = intLast + 5 - (intLast + 5) Mod 5
Set rngDest = Omega.Cells(intNext, "A").Resize(30000, 3)
To get the last column in row 1 with anything in it you should be doing
intLast = Omega.Cells(1,Omega.Columns.Count).End(xlToLeft).Column
intNext = intLast + 5 - (intLast + 5) Mod 5
Set rngDest = Omega.Cells(1,intNext).Resize(30000, 3)
Let's go through the changes one at a time:
Cells takes two parameters. The first indicates the row the second indicates the column. You want to start on the last column of the first row. So the first parameter should be 1. The second should be the number of columns in your worksheet.
Note that it is generally a good practice to specify which worksheet/workbook you are working on. This helps prevent errors if there are multiple books open or the wrong worksheet is selected.
.End will take you to the boundary of the region you are in. Since you are at the last cell on the first row you are at the far-right of the sheet. So you have tomove to the left to find the last column with values in it. hence .End(xlToLeft)
Note The documentation tells us that the parameter must be one of these values.
Now for your error message. You can often refer to this handy VBA error list for more info on what could be causing your error. I reccomend searching the error number if it is given in the error alert.
It sounds like your error is caused because Omega is not the correct type. VBA is telling you that you are trying to access a member called Cells but it cannot find it in the object. I suggest adding Omega to the watch and look at its members when the error occurs. With a little debugging you should be able to find the source of the problem!
Hopefully this helps :)
The Range() function accepts only cell references as text Strings e.g. Range("A1").
The Cells() function accepts only numeric row/column references e.g. Cells(1, 1).
In this case, you'd need to change
intLast = shtAlpha.Cells(Rows.Count, "C").End(xlUp)
to
intLast = shtAlpha.Cells(Rows.Count, 3).End(xlUp)

Sorting rows of the data into different spreadsheets based on row data Excel VBA

Basically I want to copy a row into the spreadsheet that has the same name as one of the data entries in the row.
For m = 1 To TCD.Rows.count
If Not IsEmpty(TCD.Cells(m, Mix_Design_Colmn_Number).Value) Then
TCD.Rows(m).Copy _
Worksheets(CStr(TCD.Cells(m, Mix_Design_Colmn_Number).Value)).Rows((Worksheets(CStr(TCD.Cells(m, Mix_Design_Colmn_Number).Value)).Rows.count) + 1)
End If
Next m
However, the following line give me an object defined or application defined error:
TCD.Rows(m).Copy _
Worksheets(CStr(TCD.Cells(m, Mix_Design_Colmn_Number).Value)).Rows((Worksheets(CStr(TCD.Cells(m, Mix_Design_Colmn_Number).Value)).Rows.count) + 1)
If anyone knows of a better way of doing this or can figure out the error that would be great!
Problem is with .rows.count
Look this:
debug.Print Worksheets("Sheet1").Rows.Count
Result:
1048576
if you add 1 and we got rows(1048576+1) vba a little crazy
Try different find the last row. Maybe you will find something there -> http://www.thespreadsheetguru.com/blog/2014/7/7/5-different-ways-to-find-the-last-row-or-last-column-using-vba