I need to add form field with to every cell in a table. Here is what I have so far.
Set csvTable = ActiveDocument.Tables(2)
With csvTable
For r = 1 To UBound(csvData)
.cell(r + 4, 1).Range.FormFields.Add(Range:=.cell(r + 4, 1).Range, Type:=wdFieldFormTextInput).Result = csvData(r)(0)
.cell(r + 4, 3).Range.FormFields.Add(Range:=.cell(r + 4, 3).Range, Type:=wdFieldFormTextInput).Result = csvData(r)(3)
.cell(r + 4, 5).Range.FormFields.Add(Range:=.cell(r + 4, 5).Range, Type:=wdFieldFormTextInput).Result = csvData(r)(4)
.cell(r + 4, 4).Range.FormFields.Add(Range:=.cell(r + 4, 5).Range, Type:=wdFieldFormTextInput).Result = csvData(r)(5)
.cell(r + 4, 5).Range.FormFields.Add(Range:=.cell(r + 4, 5).Range, Type:=wdFieldFormTextInput).Result = csvData(r)(6)
Next
End With
The code seems to add form fields as wanted to all the cells, but the text added ends up only in the first column (i.e. for each row, the text is iteratively updated in the first column only and I end up with csvData(r)(6) in the first column of each row). Any help would be appreciated!
Related
For i = 0 To 2
If Niz1(i) > Niz2(i) Then
a = Niz1(i)
b = Niz2(i)
Call ZamjenaNiza(a, b)
Niz1(i) = Prvi
Niz2(i) = Drugi
End If
Next i
For j = 0 To 3
Me.DataGridView(j + 1, 1) = Niz1(j)
Me.DataGridView(j + 1, 2) = Niz2(j)
Next j
End Sub
Can anyone please help me with this problem? Can't find solution, not even on Visual Basic help page! It shows me error on this 2 code lines:
Me.DataGridView(j + 1, 1) = Niz1(j)
Me.DataGridView(j + 1, 2) = Niz2(j)
It says : Value of type 'Single' cannot be converted to 'System.Windows.Forms.DataGridViewCell'
Instead of trying to assign a Single value to a DataGridViewCell object, you should assign it to the .Value property of the object ..
Me.DataGridView(j + 1, 1).Value = Niz1(j)
I have some information in the following cells: H3,H4 and H5. Exactly: H3 contains letter a, H4 contains b and H5 contains c. Now I would like to put the content of each these cells as follows:
C11=a,C12=a,C13=a,C14=a,C15=a,C16=a.
C17=b,C18=b,C19=b,C20=b,C21=b,C22=b.
C23=b, C24=c,C25=c,C26=c,C27=c,C28=c.
It can be observe that we have a step of 6.
I have tried the following code:
For s = 0 To 17
Cells(s + 11, 3).Value = Cells(CInt(3 + (s / 6)), 8)
Next
My problem is, that the cells are not filled in a correct way and also C26, C27, C28 remain empty.
Thanks!
CInt
performs a rounding and not a truncating of the integer
you should use Int
For s = 0 To 17
Cells(s + 11, 3).Value = Cells(Int(3 + (s / 6)), 8)
Next
Run it like this:
Public Sub TestMe()
Dim s As Long
For s = 0 To 17
Debug.Print Cells(s + 11, 3).Address
Debug.Print Cells(CInt(3 + (s / 6)), 8).Address
Cells(s + 11, 3).Value = Cells(CInt(3 + (s / 6)), 8)
Next s
End Sub
Then press Ctrl+G and see the printed addresses in the immediate window.
You can achieve same result without VBA.
In cell C11 put following formula and copy down to get the results you need
=INDEX($H$3:$H$5,CEILING(ROWS($C$11:C11)/6,1))
I am experiencing a problem with the outputs from my loop. As the sub is running I can see that the results from the final IF statement are being overwritten by the results from the second one. My code is structured as follows:
for i = 1 to 5
for j = 1 to 50
for each events.value in eventArray
if events.value = arrayElem then
if cells(i,j).value = "x" then
type = "col1"
elseif cells(i,j).value = "y" then
date = "col2"
elseif cells(i,j).value = "z" then
num = "col3"
end if
count = count + 1
activeworkbook.worksheets("output").cells(count + 1, 1) = type
activeworkbook.worksheets("output").cells(count + 1, 2) = date
activeworkbook.worksheets("output").cells(count + 1, 3) = num
end if
next arrayElem
if cells(i,j).value = "a" then
name = "row1"
elseif cells(i,j).value = "b" then
size = "row2"
elseif cells(i,j).value = "c" then
height = "row3"
end if
activeworkbook.worksheets("output").cells(count + 2, 1) = name
activeworkbook.worksheets("output").cells(count + 2, 2) = size
activeworkbook.worksheets("output").cells(count + 2, 3) = height
next j
next i
Obviously these are dumby variables and results, but the overall structure is the same as the real code. I can see "name","size", and "height" being printed, but then they get replaced by "type", "date", and "num". How do I prevent this from happening? Each time a new event is found I need it to print its associated characteristics printed into a new row in the "output" sheet.
Consider the following simplified version of your code:
For i = 1 To 100
If x = y Then
rowNum = rowNum + 1
Cells(rowNum + 1, 1) = "A"
End If
Cells(rowNum + 2, 1) = "B"
Next
Each time through the loop you are writing out either one or two things (two if x = y is true, one if it isn't) but you are only incrementing the row number by zero or one (one if x = y is true, zero if it isn't). Even if you know that x will always equal y, you are still trying to write two rows of information out but only increasing the row counter by one.
Assuming you are not trying to replace the "B"s in my example with the "A"s from the next iteration through the loop, you should change the code to something like:
For i = 1 To 100
If x = y Then
rowNum = rowNum + 1
Cells(rowNum, 1) = "A"
End If
rowNum = rowNum + 1
Cells(rowNum, 1) = "B"
Next
I have a nested for loop that is looking to take values in one row, generate a value based on an equation, then do the same for many rows following the first one, all while adding the value together.
Essentially, if row one has a value of 15, and row 2 and 3 return values of 10 and 12, the variable storing the total value (named genCost) will be 37.
I want to place the summed total values of genCost in a new sheet, separated by day, but when I run the code I get a Run-time 1004 error. I realize that this has something to do with the sheet that I am working on, and the sheet that I am trying to place the values into (the 2nd to last line in the code).
I understand my code may be ugly and simple, but can somebody help me troubleshoot this?
'Nested For loop for ALL OTHER DAYS of genCost...only 1 formula
For j = 2 To dayNumber
For i = 1 To increments
'IF(AND(U7=1,U6=0),R7,0)
If Cells(rowValue, 21) = 1 And Cells(rowValue - 1, 21) = 0 Then
ifValue = Cells(rowValue, 18)
Else
ifValue = 0
End If
'calculate value variable with second half of equation
value = (((Cells(rowValue, 23) * Cells(rowValue, 16) * (1 / 6)) + (Cells(rowValue, 25) * Cells(rowValue, 17) * (1 / 6)) + (Cells(rowValue, 21) * Cells(rowValue, 19) * (1 / 6)) + ifValue))
genCost = genCost + value
'set value and ifValue back to zero and step down one row and do again
value = 0
ifValue = 0
rowValue = rowValue + 1
Next i
Cells(3, j) = genCost
Dim genCostRefNum As Integer: genCostRefNum = 6
ThisWorkbook.Sheets(1).Range(Cells(genCostRefNum, 4)) = genCost
genCost = 0
Next j
Instead of this
ThisWorkbook.Sheets(1).Range(Cells(genCostRefNum, 4)) = genCost
write this
ThisWorkbook.Sheets(1).Cells(genCostRefNum, 4) = genCost
There is a default property of Cells statement, which is Value (same for Range). So your code was exactly
ThisWorkbook.Sheets(1).Range(Cells(genCostRefNum, 4).Value).Value = genCost
and after executing Cells something like:
ThisWorkbook.Sheets(1).Range(6).Value = genCost
It's good practice to always write complete statements and don't rely on default properties.
I have a code which takes data from a PicoLog 1012 and records it into an excel spreadsheet. It is working well but currently it always records 12 channels of data. This will make it slow if a lot of data is required so I would like to allow users to enter a value in a cell to define the number of channels and then skip running unnecessary code based on this.
The important parts are:
Dim values() As Integer 'number of datapoints in the array. Equal to channels * number of datapoints required.
Dim numChannels As Integer
numChannels = Worksheets("Sheet1").Range("W5").value 'this allows the user to set the number of channels
Dim samplenum As Long
samplenum = Worksheets("Sheet1").Range("W3").value 'Reads number of samples desired per channel
nValues = samplenum * numChannels
'The code apparently requires one of these lines per channel.
channels(0) = 1
channels(1) = 2
channels(2) = 3
channels(3) = 4
channels(4) = 5
channels(5) = 6
channels(6) = 7
channels(7) = 8
channels(8) = 9
channels(9) = 10
channels(10) = 11
channels(11) = 12
ReDim values(12 * Worksheets("Sheet1").Range("W3").value) 'allow a variable data array
Dim sampleInterval As Long
Dim microsecs_for_block As Long
Dim testlength As Integer
testlength = Worksheets("Sheet1").Range("W4").value
microsecs_for_block = testlength * 1000000
status = pl1000SetInterval(handle, microsecs_for_block, nValues, channels(0), numChannels)
status = pl1000Run(handle, nValues, 0)
'If there is a more efficient way to do what follows then I would LOVE to hear it. Currently logging begins long after I activate the macro.
ready = 0
Do While ready = 0
status = pl1000Ready(handle, ready)
Loop
Cells(14, "P").value = "RECORDING COMPLETE" 'indicate readiness
' Get a block of W3 readings...
' we can call this routine repeatedly
' to get more blocks with the same settings
Dim triggerIndex As Long
Dim overflow As Integer
status = pl1000GetValues(handle, values(0), samplenum, overflow, triggerIndex)
' Copy the data into the spreadsheet
For i = 0 To samplenum - 1
1: Cells(i + 4, "A").value = adc_to_mv(values(numChannels * i + 0))
2: Cells(i + 4, "B").value = adc_to_mv(values(numChannels * i + 1))
3: Cells(i + 4, "C").value = adc_to_mv(values(numChannels * i + 2))
4: Cells(i + 4, "D").value = adc_to_mv(values(numChannels * i + 3))
5: Cells(i + 4, "E").value = adc_to_mv(values(numChannels * i + 4))
6: Cells(i + 4, "F").value = adc_to_mv(values(numChannels * i + 5))
7: Cells(i + 4, "G").value = adc_to_mv(values(numChannels * i + 6))
8: Cells(i + 4, "H").value = adc_to_mv(values(numChannels * i + 7))
9: Cells(i + 4, "I").value = adc_to_mv(values(numChannels * i + 8))
10: Cells(i + 4, "J").value = adc_to_mv(values(numChannels * i + 9))
11: Cells(i + 4, "K").value = adc_to_mv(values(numChannels * i + 10))
12: Cells(i + 4, "L").value = adc_to_mv(values(numChannels * i + 11))
Next i
My current idea is to write 12 different subs for this and call each one depending on the number of channels required but I am sure there must be an easier way?
Is there some sort of "skip" command which causes lines to be ignored?
IF numChannels = 2
Then skip 3,4,5,6,7,8,9,10,11,12
Else
IF numChannels = 3
Then skip 4,5,6,7,8,9,10,11,12
Else
IF'.... et cetera
I believe the code you are looking for is GoTo. You can have those if statements and if the if statement is triggered, it will "GoTo" where ever your tag has been placed
MSDN Example
Sub SkipLines()
Dim intSkipToLine as Integer
If intSkipToLine = 1 Then Goto Line1:
If intSkipToLine = 2 Then Goto Line2:
If intSkipToLine = 3 Then Goto Line3:
If intSkipToLine = 4 Then Goto Line4:
Line1:
' first line code
Line2:
' second line code
Line3:
' thrid line code
Line4:
' fourth line code
End Sub
You can see the final solution I came up with here
It is not a perfect solution for logging data with picolog but IMHO it is better than the proprietary software if you don't need huge datasets or real-time visualisation.
Pros:
Instantly usable by anyone with excel experience; no need to use
picolog’s interface
No need to convert data for processing into excel
Automatic graphing using excel’s interface
Scope for easy further development (multisheet handling, application-specific processing)
Cons:
No realtime visualisation of data
May struggle to handle logs with over 10,000 data points