根据列标题格式化列

时间:2015-10-21 00:39:31

标签: excel-vba vba excel

我试图编写一个宏,它会根据列标题更改单元格的格式。

Header_2需要是正常情况, Header_3需要是大写的, 所有列标题都必须是大写的。

我简化了示例,但实际上我有80列,平均有3,000行,列中有空格 - 因此宏需要在没有选择或指定范围的情况下运行。

以下是我的代码到目前为止 - 虽然我不断发现“匹配不匹配”。错误(不确定如何解决)

提前感谢您提供的任何见解或帮助!

这是我的数据:

enter image description here

 Sub Proper_text()



Dim i As Integer


For i = 1 To 80
    If Cells(1, i).Value = "HEADER_2" Then

        For Each cell In Columns(i)

            If Not IsEmpty(cell) Then

                cell.Value = WorksheetFunction.Proper(cell.Value)


            End If

        Next cell

    End If
Next i



End Sub

1 个答案:

答案 0 :(得分:0)

试试这个

        public class LCDashBoard2 extends AppCompatActivity {
        TextView textViewBadge;
            Button idBtnMedicineCabinet;
            private Timer timer = new Timer();
            private TimerTask timerTask;

            TimePicker myTimePicker;
            Button buttonstartSetDialog;
            TextView textAlarmPrompt;
            final static int RQS_1 = 1;

            TimePickerDialog timePickerDialog;

            int count=0;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_lcdash_board2);

                textViewBadge=(TextView)findViewById(R.id.textViewBadge);
                idBtnMedicineCabinet=(Button)findViewById(R.id.idBtnMedicineCabinet);


        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);


                AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

            }

        }

 and my AlarmReceiver.java class is here

public class AlarmReceiver  extends BroadcastReceiver {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            Toast.makeText(arg0, "Alarm received in AlarmReceiver!", Toast.LENGTH_LONG).show();


        }
    }

结果

Public Sub Proper_text()
    Dim ws As Worksheet, ur As Range, fr As Long, lr As Long, lc As Long, x As Range

    Set ws = ActiveSheet
    Set ur = ws.UsedRange
    fr = ur.Row
    lr = ur.Row + ur.Rows.Count - 1
    lc = ur.Column + ur.Columns.Count - 1

    Application.ScreenUpdating = False

    For Each x In ur.Range(ur.Cells(fr, ur.Column), ur.Cells(fr, lc))
        x.Offset(, lc).Formula = "=UPPER(" & x.Address(False, False) & ")"
    Next

    For Each x In ur.Range(ur.Cells(fr + 1, ur.Column), ur.Cells(fr + 1, lc))

        If UCase(x.Offset(-1).Value2) = "HEADER_2" Then
            x.Offset(, lc).Formula = "=PROPER(" & x.Address(False, False) & ")"
        Else
            x.Offset(, lc).Formula = "=UPPER(" & x.Address(False, False) & ")"
        End If

    Next

    Set x = ws.Range(ws.Cells(fr + 1, lc + 1), ur.Cells(fr + 1, lc * 2))
    x.AutoFill Destination:=ur.Range(ur.Cells(fr + 1, lc + 1), ur.Cells(lr, lc * 2))

    ur.Range(ur.Cells(fr, lc + 1), ur.Cells(lr, lc * 2)).Copy
    ur.Range(ur.Cells(fr, ur.Column), ur.Cells(lr, lc)).PasteSpecial Paste:=xlPasteValues

    ur.Range(ur.Cells(fr, lc + 1), ur.Cells(fr, lc * 2)).EntireColumn.Delete

    Application.ScreenUpdating = True
    ws.Cells(1).Select
End Sub