将数字值格式化为数字

时间:2018-05-29 12:06:26

标签: vba excel-vba if-statement excel

对你来说,这是一个非常微不足道的问题。我基本上试图识别具有数值的范围中的每个单元格,然后将其格式化为“数字”(因此忽略那些包含字符串的单元格)。我找到了一个excel公式,它使用IF和TRUE / FALSE表达式来确定哪个单元符合条件,但是当在VBA中运行代码时,我似乎无法存储IF语句?

这可能非常愚蠢,因为我是VBA的新手,但会很感激所有的帮助!

以下代码:

Sub formatnumbers()

Dim rng As Range
Dim cell As Range

Set rng = ActiveSheet.Range("A1:N10")

For Each cell In rng
 cell.Select
         If cell.Formula = "=COUNT(FIND({0,1,2,3,4,5,6,7,8,9},cell))>0, TRUE, FALSE)" = True Then
            cell.NumberFormat = "0.00"
         End If

Next cell

End Sub

2 个答案:

答案 0 :(得分:3)

试试这段代码:

Sub formatnumbers()
    Dim rng As Range
    Dim cell As Range
    Set rng = ActiveSheet.Range("A1:G15")
    For Each cell In rng
        If IsNumeric(cell.Value) Then
            cell.NumberFormat = "0.000"
        End If
    Next cell
End Sub

或:

var nextDay =['2018-06-01',
       '2018-06-05',
       '2018-06-08',  
       '2018-06-07',
       '2018-06-18',
       '2018-06-17',
       '2018-05-28',
       '2018-05-29'];

   const mark = {
    [nextDay]: {selected: true, marked: true}
   };

this.state(
{
mark: mark,
})

          <Calendar
      onDayPress={this.onDayPress}      
      current={new Date()}
      minDate={'2018-05-24'}
      onMonthChange={(month) => {console.log('month changed', month)}}
      hideArrows={false}
      hideExtraDays={true}
      disableMonthChange={false}
      firstDay={1}
      hideDayNames={false}
      showWeekNumbers={false}
      onPressArrowLeft={substractMonth => substractMonth()}
      onPressArrowRight={addMonth => addMonth()}
      markedDates={this.state.mark}

          theme={{
            backgroundColor: '#ffffff',
            calendarBackground: '#ffffff',
            textSectionTitleColor: '#b6c1cd',
            selectedDayBackgroundColor: '#00adf5',
            selectedDayTextColor: '#ffffff',
            todayTextColor: '#00adf5',
            dayTextColor: '#2d4150',
            textDisabledColor: '#d9e1e8',
            dotColor: '#00adf5',
            selectedDotColor: '#ffffff',
            arrowColor: 'orange',
            monthTextColor: 'blue',
            textMonthFontWeight: 'bold',
            textDayFontSize: 16,
            textMonthFontSize: 16,
            textDayHeaderFontSize: 16
          }}
    />

答案 1 :(得分:0)

如果您的目标是将范围中的每个单元格格式化为数字,那么最简单的方法是将单元格乘以1。

'329' * 1 = 329
 329  * 1 = 329

因此,如果您遍历范围内的所有单元格并简单地乘以1,您将获得该范围内每个单元格的数值。