This question already has an answer here:
Why is 134.605*100 not equal 13460.5 in VBA Access? [duplicate]
(1 answer)
Closed 8 years ago.
Sorry for not giving more detailed title, but it is because of this special case. My google search did not give me any similar topic.
The following simple code should give a series of numbers from 0.1 to 10 with step 0.1 (I hoped at least) in column A:
Cells(1, 1) = 0.1
For i = 2 To 100
Cells(i, 1) = Cells(i - 1, 1) + 0.1
Next i
Until 5.9 it works well, but after that the result is not as expected:
instead of 6 I get 5,99999999999999
instead of 6.1 I get 6,09999999999999
instead of 6.2 I get 6,29999999999999
...
Could anyone explain what is wrong with the code or why I get this result?
Thanks!
Or simply this?
Sub Sample()
Dim i As Long
For i = 1 To 100
'~~> Change Sheet1 to respective sheet
ThisWorkbook.Sheets("Sheet1").Cells(i, 1) = i * 0.1
Next i
End Sub
Or like this
Sub Sample()
'~~> Change Sheet1 to respective sheet
With ThisWorkbook.Sheets("Sheet1").Range("A1:A100")
.Formula = "=Row()*.1"
.Value = .Value
End With
End Sub
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a column in my excel in the following format:
Column A
1.2kg
100ml
2m
200
I need to run the VBA to split the numbers and text separately into two columns as:
Column A | Column B
1.2 | kg
100 | ml
2 | m
200 |
I've also found a similar question in this site, however it isn't work for my VBA. Can anyone help me on this?
PS. I use excel 2007
Untested as am on mobile. Does it do what you want?
Option Explicit
Sub SplitValuesUnits()
' Code assumes values are on Sheet1. Change sheet name as needed.'
With thisworkbook.worksheets("Sheet1")
Dim LastRow as long
LastRow = .cells(.rows.count,"A").end(xlup).row
With .range("A1:B" & LastRow)
Dim ArrayOfValues() as variant
ArrayOfValues = .value2
Dim CharacterIndex as long
Dim RowIndex as long
For RowIndex = lbound(ArrayOfValues,1) to ubound(ArrayOfValues,1)
' Skip zero-length strings, as well as values which are already numbers and do not need splitting '
If len(ArrayOfValues(RowIndex,1)) <> 0 then
If not isnumeric(ArrayOfValues(RowIndex,1)) then
Dim CurrentValue as string
CurrentValue = ArrayOfValues(RowIndex,1)
' Loop through string backwards until we find a numeric value, at which point we assume there are no further non-numeric characters. '
For CharacterIndex = Len(CurrentValue) to 1 Step -1
If isnumeric(mid$(CurrentValue),CharacterIndex,1)) then exit for
Next CharacterIndex
If CharacterIndex >= 1 then
ArrayOfValues(RowIndex,1) = cdbl(left$(CurrentValue,CharacterIndex))
ArrayOfValues(RowIndex,2) = mid$(CurrentValue,CharacterIndex + 1)
End if
End if
End if
Next RowIndex
' Overwrite two columns with values. '
.value2 = arrayofvalues
End with
End with
End sub
This question already has answers here:
Code in VBA loops and never ends. How to fix this?
(2 answers)
Closed 5 years ago.
I am trying to delete entire rows based on whether the cell value in the D column is NULL or not. My entire code so far is:
Sub DeleteNULL()
Dim i As Long
For i = 2 To 119713
If IsEmpty(Range("Di")) = True Then
Rows([i]).EntireRow.Delete
Else
If IsEmpty(Range("Di")) = False Then
Next i
End If
End Sub
I keep getting compile errors, either If without Else or Next without For, how should I fix this?
Thanks in advance.
A few things:
Placement of a lot of syntax is off.
When adding or deleting rows, you need to loop backwards based on how Excel handles these events.
See code below:
Sub DeleteNULL()
Dim i As Long
For i = 119713 To 2 Step -1
If IsEmpty(Range("D" & i)) Then Rows([i]).EntireRow.Delete
Next i
End Sub
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi I want to generate n random numbers in VBA so that their sum must be 100 and they must be placed in such a way that 7 numbers come on first row then next 7 on second row and so on. I have tried so many solutions given on internet but nothing works for me. So anyone can suggest a solution or give me a link for the solution.
Thanks in advance.
Say we want to make 10 numbers that sum to 100. In A1 enter:
=RANDBETWEEN(1,50)
and in A2 enter:
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A1)),0)
copy A2 down through A10:
EDIT#1:
To use a macro, try:
Sub randomality()
Dim ary(1 To 10) As Double, zum As Double
Dim i As Long
Randomize
zum = 0
For i = 1 To 10
ary(i) = Rnd
zum = zum + ary(i)
Next i
For i = 1 To 10
ary(i) = ary(i) / zum
Next i
With Application.WorksheetFunction
For i = 1 To 10
Cells(i, "D").Value = Round(100 * ary(i), 0)
Next i
Cells(10, "D").Value = 100 - .Sum(Range("D1:D9"))
End With
End Sub
This puts the values in D1 through D10
The below code will do what you have asked but you have more questions you need to check over: -
Public Sub Sample()
Dim AryNumbers() As Long
Dim LngCounter As Long
ReDim AryNumbers(0)
Randomize
Do Until LngCounter = 100
AryNumbers(UBound(AryNumbers, 1)) = Int(10 * Rnd + 1)
If (LngCounter + AryNumbers(UBound(AryNumbers, 1))) > 100 Then
AryNumbers(UBound(AryNumbers, 1)) = 100 - LngCounter
Else
LngCounter = LngCounter + AryNumbers(UBound(AryNumbers, 1))
ReDim Preserve AryNumbers(UBound(AryNumbers, 1) + 1)
End If
Loop
End Sub
You didn't specify where the numbers are to be stored or if the are to be whole numbers, I have provided them in whole numbers in an array to answer the question.
You need to consider who random the number is, if you looked on the internet you'll know this is a difficult question
I honoured the extra requirement in the comments section of being between 1 and 10.
Have you ever noticed a problem dividing 2 / 60 in Excel Vba code? I am using Excel 2013. Please test the code below:
Sub test1()
Dim A As Integer
Dim B As Integer
MsgBox 2 / 60
A = 2
B = 60
MsgBox A / B
End Sub
Sub test2()
Cells(1, "a") = 2
Cells(1, "b") = 60
Cells(1, "c") = Cells(1, "a") / Cells(1, "b")
MsgBox Cells(1, "a") / Cells(1, "b")
End Sub
Test1 will generate a wrong answer for the math operation. Result should be 0,0333333, but it brings 3,3333333E-02. Test2 works with cells and it brings correct result.
What is wrong with my code? Is it a bug? I am soooo intrigued, why can't vba do the right calculation?
Thanks for the help
In number part E+XX or E-XX, XX denotes how many digits the decimal point is shifted right or left.
So in your 3.333333E-02 shift the decimal point 2 places left and you get 0.03333333
One of the points of such a notation is to spare you from zeros (0.0000012345 = 1.2345E-06) and to correctly show you only digits which are within the number precision (hypthetical 1234567890123456789 = 1.2345678E+18 (rest of digits is 'clipped').
And 5E+00 = 5 :) no shift.
More information: E notation
This question already has answers here:
Run-Time error 1004 Excel 2013
(2 answers)
Closed 6 years ago.
i am really knew to excel and am having some problems. i am trying to create an attendance sheet where for safety reasons which gets updated periodically throughout the day. on a sheet with all the possible names i have a column for different companies, names, camp, room, and on site. i have written my code so that if a person is on site than a 1 goes in the on site column and a 0 if they are off site. when a 1 appears i want their name and all other information to transfer to the attendance sheet so that the only names that appear are the ones that are on site. if they are on site i want the space to be left blank.
i have two problems with my code:
Sub onsite()
x = 3 'start at row 3
'start the loop
Do While Cells(x, 6) <> ""
'look for data with '1'
If Cells(x, 6) = "1" Then
'copy the row if it contains '1'
Worksheets("Sheet1").Rows(x).Copy
'go to main ERP. activate it
Worksheets("Sheet2").Activate
**erow = Sheet2.Cells(Rows.Count, 6).End(x1Up).Offset(1, 0).Row**
'paste data
'**ERROR OCCURS HERE**
ActiveSheet.Paste Destination:=Worksheets("Sheet2").Rows(erow)
End If
'go to all names and activate
Worksheets("AllNames").Activate
'loop through the other rows
x = x + 1
Loop
End Sub
The first problem is that after i reach the bold line i get an error '1004' message and the code stops working
The other problem is that i dont know how to change 'erow=' into code that skips a row when a person has 0 in their on site column
please help!!
Sub onsite()
Dim x as long, erow as Long
Dim shtSrc as Worksheet, shtDest as worksheet
Set shtSrc = Worksheets("Sheet1")
Set shtDest = Worksheets("Sheet2")
erow = shtDest.Cells(rows.count, 6).End(xlUp).Row+1
x = 3
Do While shtSrc.Cells(x, 6) <> ""
If shtSrc.Cells(x, 6) = "1" Then
shtSrc.Rows(x).Copy shtDest.cells(erow,1)
erow = erow+1
End If
x = x + 1
Loop
End Sub
This MS Knowledgebase article describes the cause of the error and a workaround.
Basically it's a problem when programmatically copying and pasting an entire row. You have to select only the cells in the row that are actually used.