I have the following VBA which is working fine
Worksheets(d).Cells(x, 5).Value = Application.WorksheetFunction.Max(Range("Data!E3:E7"))
This method should be nested in a FOR function such as
For i = 3 To j
Worksheets(d).Cells(x, 5).Value = Application.WorksheetFunction.Max(Range("Data!E3:E5"))
x = x + 1
Next
How can I parse the E3 and E7to be parameter based according to i meaning
E3 = "E" + i
E5 = "E" + i + 2
full example of what I'm trying:
For i = 3 To j
Worksheets(d).Cells(x, 5).Value = Application.WorksheetFunction.Max(Range("Data!E" + i + ":E" + i+2 +"))
x = x + 1
Next
Try
Sheets("Data").Range("E" & i & ":E" & (i + 2))
& is better than + in combining data of various types into a string. Also -- Range is a child of WorkSheet -- so you need to go through the appropriate Sheet object
Note that you can do this directly without a loop
As formula
Worksheets(d).Cells(x, 5).Resize(J - 3 + 1, 1).FormulaR1C1 = "=MAX(Data!R[1]C5:R[" & J - 3 + 1 & "]C5)"
As values
Worksheets(d).Cells(x, 5).Resize(J - 3 + 1, 1).Value = "=MAX(Data!R[1]C5:R[" & J - 3 + 1 & "]C5)"
Related
I am trying to overwrite wrong Excel formula.
I copied the formula from the cell into the code and placed double quotes.
Already read that I have to type two times " for getting a " into the formula. I can't tell what is really going on, because of companies compliance but the code should do it.
Imagine there are 180 components with the same 18 function in cells. By deleting some components I lost the reference and instead of writing every formula again, I try to do this with VBA.
Sub nachtrag()
Dim i As Integer
Dim j As Integer
Dim Start As Integer
Start = 4
Dim Bezug As Integer
For i = 0 To 179
Bezug = Worksheets("QK-Daten").Range("R" & ((i * 18) + 18) + Start).Value
For j = 1 To 17
Worksheets("QK-Daten").Range("Z" & j + (i * 18) + Start).Value = "=WENN(R" & Bezug & "="""";"""";SVERWEIS(R" & Bezug & ";'QK-Tabelle'!$B$3:$C$123;2;FALSCH))*(R" & j + (i * 18) + Start & "/R" & Bezug & ")+((N" & j + (i * 18) + Start & "+O" & j + (i * 18) + Start & ")*0,3+(P" & j + (i * 18) + Start & "+Q" & j + (i * 18) + Start & ")*0,1)"
Next j
Worksheets("QK-Daten").Range("z" & ((i * 18) + 18) + Start).Copy
Worksheets("QK-Daten").Range("z" & ((i * 18) + 18) + Start).PasteSpecial Paste:=xlValues
Next i
End Sub
Try writing the formula with placeholders in the string and then use Replace() to substitute the values.
Option Explicit
Sub nachtrag()
Dim ws As Worksheet, cell As Range, s As String
Dim Bezug As Long, Start As Long
Dim i As Long, j As Long, r As Long
' formula
Const F = "=IF(R<bezug>="""";"""";VLOOKUP(R<bezug>;'QK-Tabelle'!$B$3:$C$123;2;FALSE))" & _
"*(R<r>/R<bezug>)+((N<r>+O<r>)*0,3+(P<r>+Q<r>)*0,1)"
Start = 4
Set ws = Worksheets("QK-Daten")
With ws
Set cell = .Range("R" & Start)
For i = 0 To 179
Bezug = cell.Offset(18).Row 'Value
For j = 1 To 17
r = j + (i * 18) + Start
s = Replace(F, "<bezug>", Bezug)
s = Replace(s, "<r>", r)
cell.Offset(j, 8).Formula = s 'Z
Next j
' not sure what this is doing
' .Range("z" & ((i * 18) + 18) + Start).Copy
' .Range("z" & ((i * 18) + 18) + Start).PasteSpecial Paste:=xlValues
cell.Offset(18, 8).Value = cell.Offset(18, 8).Value
' next component
Set cell = cell.Offset(18)
Next i
End With
MsgBox "Done", vbInformation
End Sub
I thought I could group radio buttons by setting the LinkedCell property differently for those I wanted grouped together, however, in my code all of the buttons end up going to the same LinkedCell. I need to group RadioButtons dynamically because the numbers of buttons in the groups vary. I am working on an exam program and need to group RadioButtons for each question together, what would be the easiest way to accomplish this?
-next day
I discovered that I was not incrementing the name of the RadioButtons so every button had the same name. I also made the buttons smaller so that they did not overlap and I got everything to work when I manually placed GroupBoxes, now I have to add them from VBA and cross my fingers.
For c = 1 To ExamData(i, 7)
ws.Range("C3").Offset(rOff + z, cOff).Value = ExamData(i, a) 'write answer
rbCapt = CaptSelect(c) 'Set choice letter as caption
Set t = ws.Cells(rOff + v, 2)
Set rb = ws.OptionButtons.Add(t.Left + 20, t.Top, t.Width, t.Height)
With rb
.caption = rbCapt
.Name = "Btn" & Trim(Str(b))
.LinkedCell = "A" + Trim(Str(myRow)) '<- When myRow changes all Buttons change
End With
Well I worked through at, Adding GroupBoxes worked. Here is the final code:
For i = 1 To UBound(ExamData)
'write question number
ws.Range("B3").Offset(rOff, cOff).Value = Trim(Str(i)) + "."
myRow = ws.Range("B3").Offset(rOff, cOff - 1).Row 'row number of question
'write question
ws.Range("C3").Offset(rOff, cOff).Value = ExamData(i, 1)
'write answers & Radio Buttons
For c = 1 To ExamData(i, 7)
ws.Range("C3").Offset(rOff + z, cOff).Value = ExamData(i, a) 'write answer
rbCapt = CaptSelect(c) 'Get choice letter for radio button
Set t = ws.Cells(rOff + v, 2)
Set rb = ws.OptionButtons.Add(t.Left + 11, t.Top + 12, t.Width - 22, t.Height - 20)
With rb
.caption = rbCapt
.Name = "Btn" & Trim(Str(i)) & "-" & Trim(Str(b))
.LinkedCell = "D" + Trim(Str(myRow))
End With
v = v + 1
a = a + 1
b = b + 1
z = z + 1
Next c
'add GroupBox
pp = (myRow + 1 + ExamData(i, 7) - 1)
gbRange = "B" + Trim(Str(myRow + 1)) + ":B" + Trim(Str(pp))
Set vvv = ws.Range(gbRange)
Set gb = ws.GroupBoxes.Add(vvv.Left, vvv.Top, vvv.Width, vvv.Height)
With gb
.caption = ""
.Name = "gb" + Trim(Str(i))
End With
rOff = rOff + (ExamData(i, 7) + 2)
v = 4
z = 1
a = 2
Next i
my first post here. You guys have helped me million times, but this time I haven't managed to find the answer in google or here.
I created 2 for loops, one inside the other in Excel, shortened version here:
For r = 3 To 25
For col = rota_current_col To 100
Debug.Print "Current position:" & r & "," & col // + some code later
Next col
//some code
Next r
And first loop is not working at all. I'm not touching any of those values (r,col) in code inside loops. This debug print shows values form 3,7 to 3,100 but it's not looping to forth 'r' value.
I hope that is clear enough, thanks in advance!.
EDIT 1: Full loop as requested:
For r = 3 To 25 ' NOT WORKING :(
For col = rota_current_col To 100
Debug.Print "Current position:" & r & "," & col & " current Rota position: " & rota_current_row & "," & rota_current_col & " current Comp position: " & comp_current_row & "," & comp_current_col
Select Case Cells(rota_current_row, rota_current_col)
Case "U", "UZ", "U1", "UZ1"
If Cells(rota_current_row, rota_current_col) <> Cells(comp_current_row, comp_current_col) Then
result.Cells(current, 1) = rota.Cells(rota_current_row, 1)
result.Cells(current, 2) = rota.Cells(rota_current_row, 2)
result.Cells(current, 3) = rota.Cells(rota_current_row, 3)
result.Cells(current, 4) = rota.Cells(rota_current_row, rota_current_col)
result.Cells(current, 5) = rota.Cells(rota_current_row, rota_current_col).Address
result.Cells(current, 6) = comp.Cells(comp_current_row, comp_current_col)
result.Cells(current, 7) = comp.Cells(comp_current_row, comp_current_col).Address
current = current + 1
End If
End Select
rota_current_col = rota_current_col + 1
comp_current_col = comp_current_col + 1
Next col
rota_current_row = rota_current_row + 1
comp_current_row = comp_current_row + 1
Next r
Would you like me to paste full code?
the r will not go to 4 until col = 100. The inner loop needs to finish and then the outer loop starts.
try this
Sub yo()
r = 3
For col = rota_current_col To 100
Debug.Print "Current position:" & r & "," & col
r = r + 1
If r = 26 Then Exit Sub
Next col
End Sub
I created a sheet where users inputs a number of data to define the spacing and the number of elements inside a surface (they are pile that will be driven in the soil). The macro then calculate the position of every element within the surface and order it in column. Each column then have a variable number of element, each assigned a specific value of X and Y. I now wish to plot these elements using a scatter plot. I try to set an array with Range defined by the X and Y column calculated using the macro in a for loop. However, I keep getting error and I cannot seem to add the range value and stock them in array to do my graph. I don't understand why.
I did search over the site, did not find specifics answer to my problem (or was not able to understand maybe?).
Been looking for two days using google without success either.
Dim e As Integer
e = Cells(6, 1).Value
Dim Nbr_Range() As Long, size As Integer, w As Integer
size = Cells(6, 1).Value
ReDim Nbr_Range(size)
For i = 1 To e
Cells(3, 23 + 2 * i).Value = "X"
Cells(3, 24 + 2 * i).Value = "Y"
Cells(2, 23 + 2 * i).Value = i
Range(Cells(2, 23 + 2 * i), Cells(2, 24 + 2 * i)).Merge
Cells(3, 23 + 2 * i).HorizontalAlignment = xlCenter
Cells(3, 24 + 2 * i).HorizontalAlignment = xlCenter
Cells(2, 23 + 2 * i).HorizontalAlignment = xlCenter
Cells(3, 23 + 2 * i).Borders(xlEdgeBottom).LineStyle = xlContinuous
Cells(3, 24 + 2 * i).Borders(xlEdgeBottom).LineStyle = xlContinuous
Value = Cells(6, 2 + i).Value
For w = 1 To Value
Cells(3 + w, 23 + 2 * i).Value = Cells(14, 2 + i)
cond = Cells(6, 2 + i).Value
If cond Mod 2 = 1 Then
Delta = (cond - 1) * 0.5
Cells(3 + w, 24 + 2 * i).Value = Delta * Cells(10, 2 + i).Value * -1 + (w - 1) * Cells(10, 2 + i).Value
End If
Next w
Next i
'---work as expected untill here----
Dim test As Range
test = Range(Cells(4, 25), Cells(4, 26))''' <-- Return "Empty", why?
I would like to do something like
for q = 1 to e
Value = Cells(6, 2 + i).Value
test(q) = Range(Cells(4, 23+2*q), Cells(4+Value, 24+2*q))
next q
'--Then plot the graph using the stocked range value---
Currently I get
Run-time error '91':
Object variable or with block variable not set
I also got some other error depending on the type I use to define the variable test.
I am making a GUI in excel that allows the user to enter information into the spreadsheet just by using the GUI. I have run into a problem however. I can't seem to get the date to format right. this is the code I have for it (it being the code for the GUI:
Dim AN As Worksheet
Set AN = ThisWorkbook.Sheets("Sheet2")
Dim n As Long
n = AN.Range("A" & Application.Rows.Count).End(xlUp).Row
'AN.Range("A" & n + 1).Value ='
AN.Range("B" & n + 1).Value = Me.yyyy.Value + "-" + Me.mm.Value + "-" + Me.dd.Value
'AN.Range("C" & n + 1).Value ='
AN.Range("D" & n + 1).Value = Me.N_O_B.Value
AN.Range("E" & n + 1).Value = Me.Address_street.Value
AN.Range("F" & n + 1).Value = Me.City_info.Value
AN.Range("G" & n + 1).Value = Me.State_info.Value
AN.Range("H" & n + 1).Value = Me.Zip_info.Value
AN.Range("I" & n + 1).Value = Me.Lname.Value
AN.Range("J" & n + 1).Value = Me.Fname.Value
AN.Range("K" & n + 1).Value = Me.Minitial.Value
AN.Range("L" & n + 1).Value = Me.Suffix.Value
AN.Range("P" & n + 1).Value = Me.Busi_zip.Value
AN.Range("X" & n + 1).Value = Me.Shrt_Descript.Value
I am trying to get the date to show up in column B. It is showing up in column B, but not the way I thought it would. So in the GUI you enter the year, the month, and the date. Then I tried to make the code take the information and put it in a YYYY/MM/DD format but its not working. I have to have it in this format. Any advice would be helpful.
Force the date's raw value and then format the cell.
AN.Range("B" & n + 1).Value = dateserial(Me.yyyy.Value, Me.mm.Value, Me.dd.Value)
AN.Range("B" & n + 1).numberformat = "yyyy-mm-dd"
btw, the correct string concatenation operator is an ampersand (e.g. &). While a plus sign may work, it is best avoided, particularly when working with numbers.