Help with Next code

santnok

Board Regular
Joined
Jan 10, 2014
Messages
97
Hi

I'am trying to do this.

Have a spreedsheet that have some columns like this the table under here


[TABLE="width: 861"]
<tbody>[TR]
[TD][TABLE="class: grid, width: 100%"]
<tbody>[TR]
[TD][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[TD="align: center"]F[/TD]
[TD="align: center"]G[/TD]
[TD="align: center"]H[/TD]
[TD="align: center"]I[/TD]
[TD="align: center"]J[/TD]
[TD="align: center"]K[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Name[/TD]
[TD]Date[/TD]
[TD]Finished[/TD]
[TD][/TD]
[TD]Name[/TD]
[TD]Date[/TD]
[TD]Finished[/TD]
[TD][/TD]
[TD]Name[/TD]
[TD]Date[/TD]
[TD]Finished[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Roy[/TD]
[TD]23.4.2018[/TD]
[TD]x[/TD]
[TD][/TD]
[TD]Mette[/TD]
[TD]27.05.2018[/TD]
[TD][/TD]
[TD][/TD]
[TD]Hanne[/TD]
[TD]01.12.2018[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Mette[/TD]
[TD]27.5.2018[/TD]
[TD][/TD]
[TD][/TD]
[TD]Oskar[/TD]
[TD]30.04.2018[/TD]
[TD]x[/TD]
[TD][/TD]
[TD]Helena[/TD]
[TD]18.07.2018[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Cecilie[/TD]
[TD]03.3.2018[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Hans[/TD]
[TD]01.02.2018[/TD]
[TD]x[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Roy[/TD]
[TD]17.5.2018[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

I'am trying to get a module in

Private Sub Worksheet_Change(ByVal Target As Range)


End Sub

to do a thing. If someone write a x to column C,G or K it will run this code I have writen (that dont work)

Code:
Dim lastrowc As String
Dim lastC As String
Dim c As Variant




Dim kollone1 As Variant: kollone1 = 3
Dim kollone2 As Variant: kollone2 = 7
Dim kollone3 As Variant: kollone3 = 11
Dim startRad As String: startRad = 3


lastrowc = Cells(Rows.Count, 1).End(xlUp).Row
'lastC = Cells(1, Columns.Count).End(xlToLeft).Column


Dim col1 As Variant


For c = startRad To lastrowc
For col1 = kollone1 To kollone1


    If Cells(c, col1) = "x" Then
        Cells(c, col1) = "X"
        Cells(c, col1).Select
        With Selection.Font
            .Name = "Calibri"
            .Size = 18
        End With
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
        End With
   
    End If
Next col1


Dim col2 As Variant


For col2 = kollone2 To kollone2


    If Cells(c, col2) = "x" Then
        Cells(c, col2) = "X"
        Cells(c, col2).Select
        With Selection.Font
            .Name = "Calibri"
            .Size = 18
        End With
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
        End With
   
    End If
Next col2


Dim col3 As Variant


For col3 = kollone3 To kollone3


    If Cells(c, col3) = "x" Then
        Cells(c, col3) = "X"
        Cells(c, col3).Select
        With Selection.Font
            .Name = "Calibri"
            .Size = 18
        End With
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
        End With
   
    End If
Next col3


Next c




End Sub

But this do not work, I'am also sure that I can write the code so it is not so long?

Hope that someone understand my problem here :-)
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Not Intersect(Target, Range("C:C,G:G,K:K")) Is Nothing Then
      Application.EnableEvents = False
      If LCase(Target.Value) = "x" Then
         Target.Value = "X"
         With Target
            .Font.Name = "Calibri"
            .Font.Size = 18
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
         End With
      End If
   End If
   Application.EnableEvents = True
End Sub
 
Upvote 0
Hi Fluff

This worked like charm :-)

Thank you VERRY much for that code :-)

I'am not so good at programming, but I was trying to put the code in a module, and a simple click event, but that dident work, will the code be litlebit diffrent?
I mean if I like to make a button that check if there are x and then do the formating
 
Last edited:
Upvote 0
You're welcome & thanks for the feedback.

To have the code in a regular module & run from a button, you'll need to loop through the rows, otherwise much the same although you'll need to change Target to the cell
 
Upvote 0
You said you wanted a change event code, which is what I supplied & you said it worked.
So I'm not sure what you're asking.
 
Upvote 0
Hi

You gave me just what I wanted, but I was just curious how the code should be if I had it in a button. :-)
Becouse I have a simular task in another workbook I have. It is litlebit diffrent but I think I can use the same code, but then in a button.

Thanks for all help
 
Upvote 0
How about
Code:
Sub santnok()
   Dim Cl As Range
   
   For Each Cl In Intersect(ActiveSheet.UsedRange, Range("C:C,G:G,K:K"))
      If LCase(Cl.Value) = "x" Then
         Cl.Value = "X"
         With Cl
            .Font.Name = "Calibri"
            .Font.Size = 18
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
         End With
      End If
   Next Cl
End Sub
 
Upvote 0
Hi Fluff

Thank you so much for that code, and it worked like charm :-) Have learned somthing new now :-)
 
Upvote 0
Cross posted https://www.excelforum.com/excel-pr...3-code-to-format-spreadsheet.html#post5038324

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top