Column header for different sheet
iI have many rows having data in sheet 2 and iI want the column name of the max of a row (i.e. from column from column name of B2 to AH2 inside if loop).
Sub shanaya()
Dim j As Integer
Dim i As Integer
Dim z As Integer
Dim x As Integer
z = 35
For i = 11 To 28
For j = 2 To 19
If Sheet8.Cells(j, 1) = Sheet1.Cells(i, 1) Then
Sheet1.Cells(i, 10) = Sheet8.Cells(j, z)
Max [(Sheet8.Cells(J,2)): (Sheet8.Cells(j,z))]
Sheet1.Cells(i,13) = column header of max function
End If
Next j
Next i
End Sub
Using MATCH worksheet function will give you the column matching the MAX :
There is a +1 because your range start at col 2! ;)
Sub shanaya()
Dim j As Integer
Dim i As Integer
Dim z As Integer
Dim x As Integer
Dim ColOfMax As Integer
Dim RgToSearch As Range
z = 35
For i = 11 To 28
For j = 2 To 19
If Sheet8.Cells(j, 1) = Sheet1.Cells(i, 1) Then
Sheet1.Cells(i, 10) = Sheet8.Cells(j, z)
Set RgToSearch = Sheet8.Range(Sheet8.Cells(j, 2), Sheet8.Cells(j, z))
ColOfMax = Application.WorksheetFunction.Match(Application.WorksheetFunction.Max(RgToSearch), RgToSearch, 0) + 1
Sheet1.Cells(i, 13) = Sheet8.Cells(1, ColOfMax)
End If
Next j
Next i
End Sub
Related
So I've this challenge. But first a little information for you: p goes from 1 to 24.
When p is 1 and Cells(X,4)>0 then I want to store all the values when this is true (it might be true several times, while it loops down the 2000 rows) into a value in Worksheets("myvalues").Cells(6,11).Value = a.
After this, I want p to be 2 and then do the same, and store the sum of these into Worksheets("myvalues").Cells(7,11).Value = a.
And so on until p is 24 (incl. 24)
Option Explicit
Sub main()
Dim x As Integer
Dim rowshift As Integer
Dim a As Double
Dim b As Integer, p as Integer
For x = 2 To 2000
If Worksheets("DATA").Cells(x, 2) = p And Worksheets("DATA").Cells(x, 4) > 0 Then
a = a+ Worksheets("DATA").Cells(x, 5)
p=p+1
End If
For rowshift = 6 to 29
Worksheets("myvalues").Cells(rowshift, 11).Value = a
Might this do what you intend?
Sub Main()
Dim TargetRow As Long
Dim Spike As Double
Dim p As Integer
Dim R As Long
TargetRow = 6
For p = 1 To 24
For R = 2 To 2000
With Worksheets("DATA").Rows(R)
If (.Cells(2).Value = p) And (.Cells(4).Value > 0) Then
Spike = Spike + .Cells(5).Value
End If
End With
Next R
Worksheets("MyValues").Cells(TargetRow, 11).Value = Spike
Spike = 0
TargetRow = TargetRow + 1
Next p
End Sub
To obtain the equivalent of Excel's formula
=SUMIFS(Data!$E$2:$E$2000,Data!$B$2:$B$2000,ROW()-5,Data!$D$2:$D$2000,">0")
using VBA, I suggest you change your code to have a loop within a loop
Option Explicit
Sub main()
Dim x As Long
Dim rowshift As Long
Dim a As Double
For rowshift = 6 to 29
a = 0
For x = 2 To 2000
If Worksheets("DATA").Cells(x, 2) = rowshift - 5 And _
Worksheets("DATA").Cells(x, 4) > 0 Then
a = a + Worksheets("DATA").Cells(x, 5)
End If
Next
Worksheets("myvalues").Cells(rowshift, 11).Value = a
Next
End Sub
I think you need to set p value.
Please try this full code.
Option Explicit
Sub main()
Dim x As Integer
Dim rowshift As Integer
Dim a As Double
Dim b As Integer, p as Integer
p = 1
For x = 2 To 2000
a = 0
If p < 25 Then
If Worksheets("DATA").Cells(x, 2) = p And Worksheets("DATA").Cells(x, 4) > 0 Then
a = a+ Worksheets("DATA").Cells(x, 5)
p=p+1
End If
End If
If a > 0 Then
For rowshift = 6 to 29
Worksheets("myvalues").Cells(rowshift, 11).Value = a
Next rowshift
End If
Next x
I've tried to create an array with 2 row and 5 columns in VBA codes. Is it possible? i wrote like this
Sub robin()
Cells.Select 'this codes clears previous entries
Range("T17").Activate
Selection.ClearContents
Range("E4").Select
Dim myArray(1, 4) As Double
Dim a As Double, b As Double
Dim i As Integer
Dim j As Integer
Dim c As Double
c = 1
For a = 0 To UBound(myArray())
For b = 0 To UBound(myArray())
myArray(a, b) = c
ThisWorkbook.Sheets("Sheet1").Cells(a + 1, b + 1).Value = myArray(a, b)
c = c + 1
Next b
Next a
End Sub
But it comes with two rows and two columns. What to do?
By default UBound will return the highest index of the first dimension of an array. You'll need to set the optional parameter to 2 to get the last index of the 2nd dimension.
For b = 0 To UBound(myArray(), 2)
Sub batman()
[Sheet1!A1:E2] = [{1,2,3,4,5;6,7,8,9,10}]
End Sub
or
Sub robin()
Dim myArray(1 To 2, 1 To 5) As Double, c As Long
For c = 1 To 5
myArray(1, c) = c
myArray(2, c) = c + 5
Next
[Sheet1!A1:E2] = myArray
End Sub
Thanks all of you for your time. I've figured out my way to get what I was looking for. I rewrote the code like this:
Sub robin()
Cells.Select 'this codes clears previous entries
Range("T17").Activate
Selection.ClearContents
Range("E4").Select
Dim myArray(1, 5) As Double
Dim a As Double, b As Double
Dim i As Integer
Dim j As Integer
Dim c As Double
c = 1
For a = LBound(myArray, 1) To UBound(myArray, 1)
For b = LBound(myArray, 2) To UBound(myArray, 2)
myArray(a, b) = c
ThisWorkbook.Sheets("Sheet1").Cells(a + 1, b + 1).Value = myArray(a, b)
c = c + 1
Next b
Next a
End Sub
And that worked perfect for me. Thanks again.
I have to print the sequential 25 prime numbers after I ask the user to input a number. The code below indeed does print prime numbers but it prints empty spaces. I can't figure out how to make it print ONLY the prime numbers and no empty spaces. I have this thus far:
Public Function IsPrime(value)
Dim remainder As Double
Dim rounded As Long
Dim Max As Long
Dim j As Long
IsPrime = 0
Max = 1 + Int(value / 2)
For j = 2 To Max
rounded = Int(value / j)
remainder = (value / j) - rounded
If remainder = 0 Then
Exit For
End If
Next j
If j = Max + 1 Or value = 2 Then
IsPrime = 1
End If
If value <= 1 Then
IsPrime = 0
End If
End Function
Public Sub printprime()
Dim x As Integer
Dim row As Integer
Dim col As Integer
Dim j As Integer
Dim matrix1(1 To 5, 1 To 5) As Integer
x = InputBox("Please enter a number ")
j = 1
For row = 1 To 5
For col = 1 To 5
If IsPrime(x + j) = 1 Then
Cells(row, col) = x + j
matrix1(row, col) = Cells(row, col)
End If
j = j + 1
Next col
Next row
You just need to keep going and only record the primes:
Public Sub printprime()
Dim x As Long
Dim row As Long
Dim col As Long
Dim j As Long
x = InputBox("Please enter a number ")
row = 1
col = 1
For j = x + 1 To 9999
If IsPrime(j) Then
Cells(row, col) = j
col = col + 1
If col = 6 Then
col = 1
row = row + 1
End If
If row = 7 Then Exit Sub
End If
Next j
End Sub
Here's my version
Sub print_prime()
Dim i As Integer
Dim j As Integer
Dim if_prime As Boolean
Dim Count
Count = InputBox("Enter Max Number")
For i = 2 To Count
if_prime = True
If i = 2 Then GoTo nextItem
For j = 2 To i - 1
If i Mod j = 0 Then
if_prime = False
GoTo nextItem
End If
Next j
nextItem:
If if_prime Then Debug.Print i
Next i
End Sub
I am trying to verify that there are no error between the value in cells on column A and in Column C.
I keep getting an error saying "Next Without For"
Sub ISIN()
Dim i As Integer
Dim y As Integer
Dim t As Integer
Dim z As Integer
For i = 20 To 53
For y = 6 To 38
For t = 20 To 53
For z = 53 To 87
If Cells(i, 3) = Cells(y, 1) Then
Cells(t, 19) = "Abracadabra"
Else: Cells(z, 3) = Cells(y, 1)
Next z
Next t
Next y
Next i
End If
End Sub
you need to close the if statement before you can use the next keyword
Sub ISIN()
Dim i As Integer
Dim y As Integer
Dim t As Integer
Dim z As Integer
For i = 20 To 53
For y = 6 To 38
For t = 20 To 53
For z = 53 To 87
If Cells(i, 3) = Cells(y, 1) Then
Cells(t, 19) = "Abracadabra"
Else
Cells(z, 3) = Cells(y, 1)
end if
Next z
Next t
Next y
Next i
End Sub
How can I find a numeric number in the same cell after character. For ex After J* find number 01. I will have few rows and inside row some value will J*01 or J*08 im trying separate between character and number using instar in VBA:
Sub zz()
Dim ii As Long, z As Integer, xlastrow As Long
Dim yy As String
xlastrow = Worksheets("Sheet1").UsedRange.Rows.Count
For ii = 1 To xlastrow
yy = "J*"
z = 1
If IsNumeric(Worksheets("Sheet1").Range("B" & ii)) Then
This line separating number after J* character and pasting it to sheet2
Seprate.Find.Range("B" & ii, yy).Value = Worksheet("Sheet2").Range("A" & z)
End If
z = z + 1
Next ii
End Sub
Please try this code
' paste the values in column A.
q1w2e3r4asJ*66bvft654
1234BA
BA1234BA
xuz12354
''''' Code
Option Explicit
Sub Remove_Charecter()
Dim Last_Row As Double
Dim num As Double
Dim i As Integer
Dim j As Integer
Last_Row = Range("A65536").End(xlUp).Row
For i = 1 To Last_Row
num = 0
For j = 1 To Len(Cells(i, 1))
If IsNumeric(Mid(Cells(i, 1), j, 1)) Then
num = Trim(num & Mid(Cells(i, 1), j, 1))
End If
Next j
Cells(i, 2).Value = (num)
Next i
'MsgBox num
End Sub
'--- Output will be
123466654
1234
1234
12354
Try the below piece of codes.
Assumption
Your data that you need to separate is in Column A
There is no blank cells in your data
Trim value will be displayed in the adjacent column i.e. Column B in subsequent cells
Code :
Dim LRow As Double
Dim i As Integer
Dim j As Integer
Dim LPosition As Integer
Dim Number As Double
LRow = Range("A1").End(xlDown).Row
For i = 1 To LRow
Number = 0
LPosition = InStr(1, Cells(i, 1), "J*")
For j = (LPosition + 2) To Len(Cells(i, 1))
If IsNumeric(Mid(Cells(i, 1), j, 1)) Then
num = Trim(num & Mid(Cells(i, 1), j, 1))
End If
Next j
Cells(i, 2).Value = Number
Next i