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


ABCDEFGHIJK
1NameDateFinishedNameDateFinishedNameDateFinished
2Roy23.4.2018xMette27.05.2018Hanne01.12.2018
3Mette27.5.2018Oskar30.04.2018xHelena18.07.2018
4Cecilie03.3.2018Hans01.02.2018x
5Roy17.5.2018
6
7

<tbody>
</tbody>

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 :)

<tbody>
</tbody>
 
Last edited:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
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,215,338
Messages
6,124,349
Members
449,155
Latest member
ravioli44

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